【问题标题】:Spring MVC forms bind multiple checkboxes to List<String>Spring MVC 表单将多个复选框绑定到 List<String>
【发布时间】:2018-07-29 16:39:08
【问题描述】:

我正在使用 Spring MVC,我生成了一个用户修改/完成的表单,然后通过 POST 发回。整数(HTML 单选按钮)或布尔值(HTML 复选框)字段可以正常工作(Java 对象已正确填充),但我有几个具有相同名称和多个值的复选框;我希望将这些存储在 Java 对象的 List 结构中;我已经关注了网络示例,但它似乎不起作用。有什么想法吗?

控制器:

@RequestMapping("/generateTest")
    public String generateTest(Model model) {
        model.addAttribute("grades", subject.getGrades());
        model.addAttribute("testGenRequest", new TestGenerationRequest());
        return "generatetest";
    }

    @RequestMapping(value = "/generateTest/download.pdf", method = RequestMethod.POST)
    public String generateTestDownload(@ModelAttribute("testGenRequest") TestGenerationRequest testGen, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "redirect:/generateTest";
        }
        String[] suppressedFields = result.getSuppressedFields();
        if (suppressedFields.length > 0) {
            throw new RuntimeException("Attempting to bind disallowed fields: " + StringUtils.arrayToCommaDelimitedString(suppressedFields));
        }
        model.addAttribute("countedgrades", testGen.getSelectedGrades().size());
model.addAttribute("countedgrades", testGen.getSelectedGrades().size());
        return "pageToTestDisplayOfFormInput";
    }

    @InitBinder
    public void initialiseBinder(WebDataBinder binder) {
        binder.setAllowedFields("testNum", "selectedGrades", "orientation", "includeRepro");

    }

View (JSP) - 按预期显示给用户;内容被截断以仅显示无效的字段(selectedGrades):

<c:url var="actionURL" value="/generateTest/download.pdf"/>
                <form:form method="POST" modelAttribute="testGenRequest" action="${actionURL}" class="form-horizontal" enctype="multipart/form-data" id="testForm">
                    <form:errors path="*" cssClass="alert alert-danger" element="div"/>
                        <fieldset>
                                    <div class="col-sm-10" id="whichGrades">
                                    <c:forEach items="${grades}" var="grade">
                                        <label class="checkbox-inline btn btn-default" for="whichGrade-${grade.grade}">
                                            <form:checkbox id="whichGrade-${grade.grade}" value="${grade.grade}" path="selectedGrades"/>
                                           ${grade.grade}
                                            </label>
                                    </c:forEach>

                                </div>
                            </div>

                          </fieldset>             
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit">Confirm</button>

                </form:form>

模型属性支持对象TestGenerationRequest.class:

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.stereotype.Component;

@Component
@XmlRootElement(name = "testGenerationRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestGenerationRequest implements Serializable {

    private final static long serialVersionUID = 934343334534L;
    @JsonIgnore
    public static final int FORMAT_PORTRAIT = 1;
    @JsonIgnore
    public static final int FORMAT_LANDSCAPE = 2;

    private int testNum;
    private List<String> selectedGrades;
    private int orientation;
    private boolean includeRepro;

    public TestGenerationRequest() {
        super();
        selectedGrades = new ArrayList<String>();
        this.orientation = TestGenerationRequest.FORMAT_PORTRAIT; 
        this.includeRepro = false; 
    }

    public int getTestNum() {
        return testNum;
    }

    public void setTestNum(int testNum) {
        this.testNum = testNum;
    }

    public int getOrientation() {
        return orientation;
    }

    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }

    public boolean isIncludeRepro() {
        return includeRepro;
    }

    public void setIncludeRepro(boolean includeRepro) {
        this.includeRepro = includeRepro;
    }

    public List<String> getSelectedGrades() {
        return selectedGrades;
    }

    public void setSelectedGrades(List<String> selectedGrades) {
        this.selectedGrades = selectedGrades;
    }

}

任何帮助将不胜感激。期望的结果是控制器的 generateTestDownload 方法中处理的 TestGenerationRequest 包含一个列表,其中包含与用户在视图中勾选的复选框相对应的元素(这些可能是整数或字符串)。目前的实际结果是 selectedGrades 始终为空(0 个元素)。谢谢。

【问题讨论】:

    标签: java spring spring-mvc spring-form


    【解决方案1】:

    我找到了解决方案,以供未来的读者受益。

    在视图中,有一个 enctype="multipart/form-data" 不是必需的;删除它,一切正常!

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多