【问题标题】:Map checkboxes to List using Sprint Boot and Thymeleaf使用 Spring Boot 和 Thymeleaf 将复选框映射到列表
【发布时间】:2016-06-11 07:47:51
【问题描述】:

我有一个使用 ThymeleafSpring Boot 1.3.0 应用程序。我在页面上有一个表单,允许用户上传文件。我想添加一些复选框也返回到我的控制器。

我还没有找到一个很好的例子来做复选框。我想我必须在我的模型中定义一个列表,并让 Thymeleaf 显示所有复选框,但我无法使其工作。

这是我的控制器:

@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
    }
}

这里是我的 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>

复选框列表正确构建,但是当我选择其中任何一个时,我的控制器模型中没有显示任何值。

有人可以指点我正确的方向,将选定复选框的列表返回给我的控制器吗?

【问题讨论】:

  • 我的解决方案有效吗?

标签: spring-mvc spring-boot thymeleaf


【解决方案1】:

我已经修复了你的代码。

控制器

我使用initValues() 方法用值填充model

我还在handleFileUpload() 方法中添加了@RequestParam List&lt;String&gt; searchValues 参数。

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";
    }
}

上传.html

我将&lt;label th:text="#{${search}}"&gt;&lt;/label&gt; 固定为&lt;label th:text="${search}"&gt;&lt;/label&gt;

我还修复了&lt;form&gt;&lt;input type="checkbox"&gt; 标签。

<!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>

【讨论】:

  • 完美,谢谢!我刚刚实现,我得到了我想要的 searchValues 列表。非常感谢您的帮助!
  • 工作就像一个魅力! :)
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2020-03-30
  • 2022-08-06
  • 2011-10-13
  • 1970-01-01
  • 2021-06-24
  • 2016-07-13
  • 2020-11-26
相关资源
最近更新 更多