Spring @Valid form:checkboxes项不能为null

时间:2018-07-03 16:06:52

标签: spring checkbox null hibernate-validator

我正在尝试使用form:checkboxes,@ Valid批注和bindingResult创建基本表单,但是尽管list不为null,但它返回错误。 当我正在读取代码时,sysout将打印正确的列表大小和正确的复选框大小。

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/view/home.jsp] at line [24]

21:                 <form:options items="${theCountries}"/>
22:             </form:select><br><br>
23: 
24:     Code language: <form:checkboxes path="languages" items="${programmingLanguages}"/><br>
25:     Operating System: <form:radiobuttons path="operatingSystems" items="${operatingSystems}"/>
26: 
27:     <input type="submit" name="go next">


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.IllegalArgumentException: 'items' must not be null
    org.springframework.util.Assert.notNull(Assert.java:115)
    org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag.setItems(AbstractMultiCheckedElementTag.java:84)
    org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_form_005fcheckboxes_005f0(home_jsp.java:416)
    org.apache.jsp.WEB_002dINF.view.home_jsp._jspService(home_jsp.java:206)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

要证明列表不为空: app photo link

这是代码:

ReadFromProperties.class

@Component
public class ReadPropertiesImpl implements ReadProperties {

    @Value("#{${student.countrylist}}")
    private HashMap<String,String> countries;

    @Value("#{'${student.languages}'.split(',')}")
    private List<String> languages;

    @Value("#{'${student.operatinsystems}'.split(',')}")
    private List<String> operatingSystems;

    @Override
    public HashMap<String, String> readCountries() {
        return countries;
    }

    @Override
    public List<String> readLanguages() {
        return languages;
    }

    @Override
    public List<String> readOperatingSystems() {
        return operatingSystems;
    }
}

HomeController.class

@Controller
@RequestMapping("/homeController")
public class HomeController {

    @Autowired
    ReadProperties readProperties;


    @GetMapping(value = "/home")
    public String homePage(Model model) {
        model.addAttribute("student", new Student());
        model.addAttribute("theCountries", readProperties.readCountries());
        model.addAttribute("programmingLanguages", readProperties.readLanguages());
        model.addAttribute("operatingSystems", readProperties.readOperatingSystems());
        return "home";
    }

    @GetMapping(value = "processForm")
    public String processForm(@Valid @ModelAttribute("student") Student student, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            System.out.println("has errors");
            return "home";
        } else {
            System.out.println("no errors");
            return "studInfo";
        }
    }
}

Student.class

@Component
public class Student {

    @NotNull(message = "is Required")
    @Pattern(regexp = "/^[a-z ,.'-]+$/i")
    private String fName;

    private String lName;
    private String country;
    private List<String> languages;
    private List<String>  operatingSystems;

    public Student() {
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public List<String> getLanguages() {
        return languages;
    }

    public void setLanguages(List<String> languages) {
        this.languages = languages;
    }

    public List<String> getOperatingSystems() {
        return operatingSystems;
    }

    public void setOperatingSystems(List<String> operatingSystems) {
        this.operatingSystems = operatingSystems;
    }

}

home.jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form:form action="/homeController/processForm" modelAttribute="student" method="get">
    Enter First Name: <form:input path="fName"/><br>
    <form:errors path="fName"/>

    Enter Last Name" <form:input path="lName"/><br>
    Country: <form:select path="country"><br>
                <form:options items="${theCountries}"/>
            </form:select><br><br>

    Code language: <form:checkboxes path="languages" items="${programmingLanguages}"/><br> //error here :(
    Operating System: <form:radiobuttons path="operatingSystems" items="${operatingSystems}"/><br>

    <input type="submit" name="go next">
</form:form>
</body>
</html>

代码源https://github.com/carrothead/SpringMvcJavaConfig.git

0 个答案:

没有答案