1
回答
spring3用@valid注解绑定验证时,BindingResult中显示不出错误
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

domain类:

package domain;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;


import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
public class Register {
static  int a=0;
@NotEmpty
@NotNull
@Email(message="邮箱填写错误!")
@Pattern(regexp="w{10,20}")
private String e_mail;
@Size(max=6,min=1,message="姓名错误!")
@Pattern(regexp="S{1,30}")
private String userName;
@Pattern(regexp="[a-zA-z0-9]{6}" ,message="密码为6为字母或数字!")
@NotEmpty(message="密码不能为空!")
private String userPassword;
public String getE_mail() {
return e_mail;
}

public Register() {
super();
// System.out.println(a++);
}


public Register(String e_mail, String userName, String userPassword) {
super();
this.e_mail = e_mail;
this.userName = userName;
this.userPassword = userPassword;
}


public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

}


2.控制类:

package web;


import javax.validation.Valid;


import org.springframework.stereotype.Controller;
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 domain.Register;


@Controller
@RequestMapping("/user")
public class UserController {
@ModelAttribute("user")
public Register getUser(){
Register register  = new Register("", "DD-AB-123", "45678979");
return register;
}
@RequestMapping(value="/register", method=RequestMethod.POST)
public String registerPost(@Valid @ModelAttribute("user")Register user,
BindingResult bindingResult){
// bindingResult.rejectValue("e_mail", "user.e_mail", "不能为空!");
System.out.println(bindingResult.getObjectName());
System.out.println(bindingResult.getErrorCount());
System.out.println(bindingResult.getFieldValue("e_mail"));
System.out.println(bindingResult.getFieldError("e_mail"));

if(bindingResult.hasErrors()){
return "register";
}else{

return "index";
}

}
@RequestMapping(value="/register", method=RequestMethod.GET)
public String register( ){
return "register";
}
}


3.mvc配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 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.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     <!-- 注解扫描包 -->
<context:component-scan base-package="web" />
<!-- 开启注解 -->

<mvc:annotation-driven/>

<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**"/>  
<mvc:resources location="/js/" mapping="/js/**"/>   



<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 上传文件设置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="defaultEncoding" value="utf-8" />
     <property name="maxUploadSize" value="10485760000" />
     <property name="maxInMemorySize" value="40960" />
</bean>
 </beans>  

结果无论我在表单输入什么,都没错误显示

举报
有点笨
发帖于3年前 1回/13K+阅
共有1个答案 最后回答: 3年前

Spring在处理Validation时,会有三种情况发生:1)验证通过,控制器方法正常执行。

2)验证失败,即输入的信息未满足验证条件,BindingResult中会获得错误信息。

3)验证过程抛出异常,可能是你定义的验证条件有问题,比如将@NotEmpty 用到int类型,或者条件相互冲突,或者验证过程中类型转换错误等,导致有异常抛出。

其中1)2)都是在Validation的控制之内,第三种情况不同于第二种情况,此时验证没有得到有效执行,BindingResult也就不会生成,控制器方法也就不会正常执行。

所以你的情况应该是第三种情况,你可以删除其中的一些注解,应该不难定位问题所在。

--- 共有 1 条评论 ---
有点笨错误原因是在部署时没将@Valid需要的包导入 3年前 回复
顶部