栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号