Jsp 表单验证在Spring MVC中不起作用

Jsp 表单验证在Spring MVC中不起作用,jsp,validation,spring-mvc,annotations,Jsp,Validation,Spring Mvc,Annotations,我是春季MVC的新手。我需要在tag的帮助下验证表单。但它并不是在验证输入字段。请帮帮我 Student.java public class Student { @Size(min=2,max=30) private String studentName; @NotNull private String studentHobby; public String getStudentName() { return studentName;

我是春季MVC的新手。我需要在tag的帮助下验证表单。但它并不是在验证输入字段。请帮帮我

Student.java

public class Student {

    @Size(min=2,max=30)
    private String studentName;

    @NotNull
    private String studentHobby;

    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getStudentHobby() {
        return studentHobby;
    }
    public void setStudentHobby(String studentHobby) {
        this.studentHobby = studentHobby;
    }
}
StudentController.java

@Controller
public class StudentController {

    @RequestMapping(value="/admissionForm.html", method = RequestMethod.GET)
    public ModelAndView getAdmissionForm() {
        ModelAndView model = new ModelAndView("AdmissionForm");
        model.addObject("student1",new Student());
        return model;
    }

    @RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
    public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student1") Student student1,BindingResult result) {

        if (result.hasErrors()) {
            ModelAndView model1 = new ModelAndView("AdmissionForm");
            return model1;
        }
        ModelAndView model = new ModelAndView("AdmissionSuccess");
        return model;
    }

}
Spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


  <context:component-scan base-package="com.ram.StudentController" />
  <mvc:annotation-driven/>

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
 </bean>

 <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

        <property name="basename" value="/WEB-INF/studentmessages" />
    </bean>
</beans>

这里是示例代码。提前感谢。

绑定错误存储在result.getModel中,但是通过创建ModelAndView实例,您正在破坏该模型,您应该像ModelandViewAppmissionForm一样,创建result.getModel


我试过了,但不起作用。它没有显示任何错误,而是返回到“承认成功”页面。
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
    <h1>User Registration</h1>
    <form action="/FirstSpringMVCProject/submitAdmissionForm.html"
        method="post">


        <spring:bind path="student1.studentName">
             <input value="${status.value}" name="${status.expression}">
        <c:if test="${status.error}">
            Error codes:
            <c:forEach items="${status.errorMessages}" var="error">
                <c:out value="${error}"/>
            </c:forEach>
        </c:if>

        </spring:bind>
        <spring:bind path="student1.studentHobby">
             <input value="${status.value}" name="${status.expression}">
        <c:if test="${status.error}">
            Error codes:
            <c:forEach items="${status.errorMessages}" var="error">
                <c:out value="${error}"/>
            </c:forEach>
        </c:if>
        </spring:bind>
<input type="submit" value="Submit this form by clicking here" />
    </form>

</body>
</html>
Size.student1.studentName = please enter a value for {0} field between {2} and {1} characters.
NotNull.student1.studentHobby = HObby should not be Enmpty
@RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
    public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student1") Student student1,BindingResult result) {

        if (result.hasErrors()) {
            ModelAndView model1 = new ModelAndView("AdmissionForm", result.getModel());
            return model1;
        }
        ModelAndView model = new ModelAndView("AdmissionSuccess");
        return model;
    }