我是Spring MVC的新手。我正在使用<spring:Bind>
标记用于表单验证。但它没有显示任何错误。我为这个问题搜索了很多解决方案,但我无法得到任何正确的解决方案。请帮忙。
这里是示例代码
分派器-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.gontuseries.studentadmissioncontroller" />
<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>
Student.java
public class Student {
@Size(min=2,max=30)
private String studentName;
private String studentHobby;
// corresponding getter and setter
}
Phone.java
public class Phone {
private Integer phoneNumber;
//getter and setter
}
StudentController.java
@Controller
public class StudentAdmissionController {
@RequestMapping(value="/admissionForm.html", method = RequestMethod.GET)
public ModelAndView getAdmissionForm() {
ModelAndView model = new ModelAndView("AdmissionForm");
model.addObject("student1",new Student());
model.addObject("phone",new Phone());
return model;
}
@RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student1") Student student1, @ModelAttribute("phone") Phone phone, BindingResult result) {
if (result.hasErrors()) {
ModelAndView model1 = new ModelAndView("AdmissionForm");
return model1;
}
ModelAndView model = new ModelAndView("AdmissionSuccess");
return model;
}
}
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>Student Registration</h1>
<form action="/FirstSpringMVCProject/submitAdmissionForm.html"
method="post">
<form:errors path="student1.studentName" />
<spring:bind path="student1.studentName">
<input type="text" name="${status.expression}"
value="${status.value}">
<br />
<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 type="text" name="${status.expression}"
value="${status.value}">
<br />
<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="phone.phoneNumber">
<input type="text" name="${status.expression}"
value="${status.value}">
<br />
</spring:bind>
<input type="submit" value="Submit this form by clicking here" />
</form>
</body>
</html>
AdmissionSucess.jsp
<html>
<body>
<h3>Congratulations!! Registered Sucessfully</h3>
<table>
<tr>
<td>Student Name :</td>
<td>${student1.studentName}</td>
</tr>
<tr>
<td>Student Hobby :</td>
<td>${student1.studentHobby}</td>
</tr>
<tr>
<td>Phone :</td>
<td>${phone.phoneNumber}</td>
</tr>
</table>
</body>
</html>
如果在表单验证过程中出现任何错误,请帮我解决问题以显示错误消息。提前谢谢。