我正在努力学习spring MVC。我有一个用户注册表单,其中包含一些要应用的验证。我为它编写了一个控制器,并在模型类上使用了javax验证注释,即UserBo。
我使用Spring MVC标记来显示@Valid注释找到的任何错误。但是,错误在屏幕上不可见。
以下是我的设置中的文件。 型号:UserBO
package com.shailesh.beans;
import javax.validation.constraints.NotNull;
public class UserBO {
public enum MaritalStatus {
SINGLE, MARRIED
}
@Override
public String toString() {
return "UserBO [firstName=" + firstName + ", lastName=" + lastName
+ ", id=" + id + ", phone=" + phone + ", maritalStatus="
+ maritalStatus + "]";
}
@NotNull(message="First Name can not be null")
private String firstName;
@NotNull
private String lastName;
@NotNull
private Long id;
@NotNull
private String phone;
@NotNull
private MaritalStatus maritalStatus;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public MaritalStatus getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(MaritalStatus maritalStatus) {
this.maritalStatus = maritalStatus;
}
}
Controller:AllPathController
package com.shailesh.controller;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.shailesh.beans.UserBO;
@Controller
public class AllPathController {
@RequestMapping(value="/register", method=RequestMethod.GET)
public String register(Model model) {
model.addAttribute("userBo", new UserBO());
return "registerform";
}
@RequestMapping(value="/register", method=RequestMethod.POST)
public String register(@Valid @ModelAttribute("userBo") UserBO userBo, BindingResult result, Model model) {
if(result.hasErrors()) {
System.out.println("Input has some errors :"+result.toString());
//model.addAttribute("userBo", userBo);
return "registerform";
}
System.out.println("Input user details :" +userBo);
return "helloboss";
}
@RequestMapping(value="/homepage", method=RequestMethod.GET)
public String show() {
return "homepage";
}
@RequestMapping(value="/helloboss.do", method=RequestMethod.GET)
public String show2() {
System.out.println("get");
return "helloboss";
}
@RequestMapping(value="/helloboss.do", method=RequestMethod.POST)
public String show3() {
System.out.println("post");
return "hellobosspost";
}
}
Spring context.xml文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<mvc:annotation-driven />
<!-- <context:component-scan base-package="com.shailesh.controller.*" /> -->
<bean id="allPathController" class="com.shailesh.controller.AllPathController" />
<bean id="resolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
</beans>
自己的registerform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.error {
color: red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Registration Form</h2>
<sf:form method="POST" commandName="userBo">
First Name : <sf:input path="firstName" />
<br />
<sf:errors path="firstName" element="div"/>
Last Name : <sf:input path="lastName" />
<br />
<sf:errors path="lastName" />
Phone : <sf:input path="phone" />
<br />
<sf:errors path="phone" />
Marital Status :
<sf:select path="maritalStatus">
<sf:options path="maritalStatus" />
</sf:select>
<sf:errors path="maritalStatus" />
<input type="submit" value="Register" />
</sf:form>
</body>
</html>
我的观察是,当我给路径值为“*”时,它有效。但是,当我使用一些特定的值ex。 firstName,它不显示任何错误。我在这种情况下尝试过调试。我可以看到错误在BindingResult中填充但未在UI上显示。
任何帮助都是预先确定的。感谢。