[英]Spring validation with Javax.validation redirects to 400
I am working on a Spring-MVC application, and I want to validate some data. 我正在开发Spring-MVC应用程序,我想验证一些数据。 Currently I am able to validate the data with no problems.
目前,我能够毫无问题地验证数据。 Only thing is if the data is invalid, I would like to go to another JSP page, which is not happening right now.
唯一的是,如果数据无效,我想转到另一个JSP页面,该页面目前没有发生。 Instead I get an Apache 400 error, request was sent syntactically incorrect.
相反,我收到一个Apache 400错误,语法上发送的请求不正确。 Can anyone tell me what all is remaining to implement in validation.
谁能告诉我在验证中还剩下什么呢?
Controller : 控制器:
@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 : 实体 :
@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 : 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 Servlet-context.xml
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
Pom.xml 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>
Any pointers would be nice. 任何指针都很好。 Mainly I would like to avoid going on Apache 400, but just display what exactly went wrong with the input field.
主要是我想避免使用Apache 400,而只显示输入字段到底出了什么问题。
This is might be because of public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,Model model,BindingResult bindingResult)
signature . 这可能是由于
public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,Model model,BindingResult bindingResult)
签名引起的。
BindingResult
must follow @ModelAttribute
as method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them.That is why when the data is invalid spring is not able to bind errors to BindingResult
and throws 400 error. BindingResult
必须遵循@ModelAttribute
因为方法签名可能具有多个模型对象,并且Spring将为每个模型对象创建一个单独的BindingResult实例,这就是为什么当数据无效时spring不能将错误绑定到BindingResult
并引发400错误。
Try changing method signature to public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,BindingResult bindingResult,Model model)
. 尝试将方法签名更改为
public String addPerson(@Valid Person person,@ModelAttribute("person") Person p,BindingResult bindingResult,Model model)
。
Read more on BindingResult . 阅读更多关于BindingResult的信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.