【问题标题】:Form validation not working in Spring MVC表单验证在 Spring MVC 中不起作用
【发布时间】:2015-01-29 16:00:13
【问题描述】:

我是 Spring MVC 的新手。我需要在<Spring:bind> 标签的帮助下验证表单。但它没有验证输入字段。请帮我。

学生.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>

AdmissionForm.jsp

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

studentmessages.properties

Size.student1.studentName = please enter a value for {0} field between {2} and {1} characters.
NotNull.student1.studentHobby = HObby should not be Enmpty

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

【问题讨论】:

    标签: jsp validation spring-mvc annotations


    【解决方案1】:

    绑定错误存储在result.getModel() 中,但是通过创建您的 ModelAndView 实例,您正在破坏该模型,您应该像ModelAndView("AdmissionForm", result.getModel()); 那样做

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

    【讨论】:

    • 我试过了,但它不起作用。它没有显示任何错误,而是返回到AdmissionSucecess 页面。
    猜你喜欢
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多