使用Sprint Boot和Thymeleaf将复选框映射到列表

使用Sprint Boot和Thymeleaf将复选框映射到列表,第1张

使用Sprint Boot和Thymeleaf将复选框映射到列表

我已经修复了您的代码。

控制者

我使用

initValues()
方法来填充
model
值。

我也

@RequestParam List<String> 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;@Controllerpublic 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";    }}

upload.html

固定

<label th:text="#{${search}}"></label>
<labelth:text="${search}"></label>

我也固定

<form>
<input type="checkbox">
标签。

<!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"  value="Submit Customer Data"/></p>    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/></form></body></html>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4911550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存