我正在开发一个 Spring-MVC 应用程序,我想验证一些数据。目前我能够毫无问题地验证数据。唯一的问题是如果数据无效,我想转到另一个 JSP 页面,但现在没有发生。相反,我收到 Apache 400 错误,请求发送的语法不正确。谁能告诉我在验证中还剩下什么要实施。
控制器 :
@RequestMapping(value = "/", method = RequestMethod.GET)
public String listPersons(Model model) {
Person person = personService.getCurrentlyAuthenticatedUser();
if(!(person==null)){
return "redirect:/canvas/list";
} else {
model.addAttribute("person", new Person());
// model.addAttribute("listPersons", this.personService.listPersons());
model.addAttribute("notices",new Notes());
model.addAttribute("canvases",new Canvas());
return "person";
}
}
@RequestMapping(value= "/person/add", method = RequestMethod.POST)
public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,Model model,BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "redirect:/";
}
this.personService.addPerson(p);
return "redirect:/";
}
实体 :
@Entity
@Table(name="person")
public class Person implements UserDetails{
@NotEmpty @Email
@Column(name = "username")
private String username;
@NotEmpty(message = "Please enter password")
@Column(name = "password")
private String password;
@Size(min = 2,max = 30)
@Column(name = "firstname")
private String firstName;
@Size(min = 2,max = 50)
@Column(name = "secretquestion")
private String secretquestion;
@Size(min = 2,max = 500)
@Column(name = "secretanswer")
private String secretanswer;
}
JSP:
<tr>
<td>
<form:label path="firstName">
<spring:message text="FirstName"/>
</form:label>
</td>
<td>
<form:input path="firstName" />
</td>
<td><form:errors path="firstName"/>Please enter Firstname properly</td>
</tr>
<tr>
<td>
<form:label path="username">
<spring:message text="Email"/>
</form:label>
</td>
<td>
<form:input path="username" />
</td>
<td><form:errors path="username"/>Please enter Email properly</td>
</tr>
<tr>
<td>
<form:label path="password">
<spring:message text="Password"/>
</form:label>
</td>
<td>
<form:input path="password" />
</td>
<td><form:errors path="password"/>Please enter password properly</td>
</tr>
<tr>
<td>
<form:label path="secretquestion">
<spring:message text="secretquestion"/>
</form:label>
</td>
<td>
<form:input path="secretquestion" />
</td>
<td><form:errors path="secretquestion"/>Please enter secretquestion properly</td>
</tr>
<tr>
<td>
<form:label path="secretanswer">
<spring:message text="secretanswer"/>
</form:label>
</td>
<td>
<form:input path="secretanswer" />
</td>
<td><form:errors path="secretanswer"/>Please enter secretanswer properly</td>
</tr>
Servlet-context.xml
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
Pom.xml
<!-- Validation -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
任何指针都会很好。主要是我想避免使用 Apache 400,而只是显示输入字段到底出了什么问题。