Spring mvc 使用Sprint引导和Thymeleaf将复选框映射到列表
我有一个Spring Boot 1.3.0应用程序,它使用Thymeleaf。我在页面上有一个表单,允许用户上传文件。我想添加一些复选框也返回到我的控制器 我还没有找到一个很好的例子,如何这样做复选框。我想我必须在我的模型中定义一个列表,并让Thymeleaf显示所有复选框,但我一直无法使它工作 这是我的控制器:Spring mvc 使用Sprint引导和Thymeleaf将复选框映射到列表,spring-mvc,spring-boot,thymeleaf,Spring Mvc,Spring Boot,Thymeleaf,我有一个Spring Boot 1.3.0应用程序,它使用Thymeleaf。我在页面上有一个表单,允许用户上传文件。我想添加一些复选框也返回到我的控制器 我还没有找到一个很好的例子,如何这样做复选框。我想我必须在我的模型中定义一个列表,并让Thymeleaf显示所有复选框,但我一直无法使它工作 这是我的控制器: @Controller public class CustomerDataController { private static final String SEARCH_TY
@Controller
public class CustomerDataController {
private static final String SEARCH_TYPES = "searchTypes";
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String displayUpload(Model model) {
initModel( model );
return "upload";
}
private void initModel(Model model) {
model.addAttribute( UPLOAD, null );
// the values to display as check box title & values
model.addAttribute(SEARCH_TYPES, Arrays.asList("Search A", "Search B"));
// list to store what the user checks on the UI
model.addAttribute("searchValue", new ArrayList<>());
model.addAttribute( customerResults );
}
@Transactional
@RequestMapping(value = "/userFile", method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("myFile") MultipartFile file, Model model, Authentication authentication) {
// looking to get searchValue list, but not sure this is right
}
}
@控制器
公共类CustomerDataController{
私有静态最终字符串搜索\u TYPES=“searchTypes”;
@RequestMapping(value=“/upload”,method=RequestMethod.GET)
公共字符串显示上载(模型){
初始模型(模型);
返回“上传”;
}
私有void initModel(模型){
model.addAttribute(上传,空);
//要显示为复选框标题和值的值
addAttribute(搜索类型,数组.asList(“搜索A”,“搜索B”));
//列表以存储用户在UI上检查的内容
addAttribute(“searchValue”,新的ArrayList());
model.addAttribute(customerResults);
}
@交易的
@RequestMapping(value=“/userFile”,method=RequestMethod.POST)
公共字符串handleFileUpload(@RequestParam(“myFile”)多部分文件,模型,身份验证){
//正在查看searchValue列表,但不确定这是否正确
}
}
这里是我的html的重要部分:
<form onsubmit="return validate(this)" action="userFile"
th:action="@{/userFile}" method="post" enctype="multipart/form-data">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<ul>
<li th:each="search : ${searchTypes}">
<input type="checkbox" th:field="*{searchValue}" th:value="${search}"/>
<label th:text="#{${search}}"></label>
</li>
</ul>
<p><input type="file" name="myFile" id="myFile"/></p>
<p><input type="submit" class="btn btn-success" value="Submit Customer Data"/></p>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
-
复选框列表正确构建,但当我选择其中任何一个复选框时,控制器模型中不会显示任何值
有人能告诉我一个正确的方向,将选中的复选框列表返回给我的控制器吗?我已经修复了您的代码 控制器 我使用
initValues()
方法用值填充model
我还将@RequestParam List searchValues
参数添加到handleFileUpload()
方法中
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.List;
@Controller
public class CustomerDataController {
private static final String SEARCH_TYPES = "searchTypes";
@ModelAttribute
public void initValues(Model model) {
model.addAttribute(SEARCH_TYPES, Arrays.asList("Search A", "Search B"));
}
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String displayUpload() {
return "upload";
}
@RequestMapping(value = "/userFile", method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("myFile") MultipartFile file,
@RequestParam List<String> searchValues) {
// here you can use searchValues and file
return "result";
}
}
import org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.multipart.MultipartFile;
导入java.util.array;
导入java.util.List;
@控制器
公共类CustomerDataController{
私有静态最终字符串搜索\u TYPES=“searchTypes”;
@模型属性
公共void初始值(模型){
addAttribute(搜索类型,数组.asList(“搜索A”,“搜索B”));
}
@RequestMapping(value=“/upload”,method=RequestMethod.GET)
公共字符串displayUpload(){
返回“上传”;
}
@RequestMapping(value=“/userFile”,method=RequestMethod.POST)
公共字符串handleFileUpload(@RequestParam(“myFile”)多部分文件,
@RequestParam列表搜索值){
//在这里,您可以使用SearchValue和file
返回“结果”;
}
}
upload.html 我将
修改为
我还修复了
和
标记
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
lang="en"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>Upload</title>
</head>
<body>
<form th:action="@{/userFile}" method="post" enctype="multipart/form-data">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<ul>
<li th:each="search : ${searchTypes}">
<input type="checkbox" name="searchValues" th:value="${search}"/>
<label th:text="${search}"></label>
</li>
</ul>
<p><input type="file" name="myFile" id="myFile"/></p>
<p><input type="submit" class="btn btn-success" value="Submit Customer Data"/></p>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
</body>
</html>
上传
-
我的解决方案有效吗?太好了,谢谢!我刚刚实现了,我得到了一个搜索值列表,就像我想要的一样。我真的很感谢你的帮助!工作得很有魅力!:)