Spring MVC表单将多个复选框绑定到List <string>

时间:2018-07-29 16:39:08

标签: java spring spring-mvc spring-form

我正在使用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");

    }

视图(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包含一个List,其中包含与用户在视图中打勾的复选框相对应的元素(这些元素可以是整数或字符串)。目前的实际结果是selectedGrades始终为空(0个元素)。谢谢。

1 个答案:

答案 0 :(得分:1)

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

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