标签:
@RequestMapping("/add2")
public String addStudentValid(@Valid @ModelAttribute("s") Student s,BindingResult result){
if(result.hasErrors()){
List<FieldError> fieldErrors = result.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
log.info("errors --"+fieldError.getField()+fieldError.getDefaultMessage());
}
}
log.info("s.name="+s.getName());
log.info("s.age="+s.getAge());
return "success";
}
import javax.validation.constraints.Min;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
public class Student {
@NotEmpty
@Length(min=4,message="{stu.name}")
private String name;
@Min(value=18,message="{stu.age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "toString - name="+name+";age="+age;
}
}
需要开启注解 <mvc:annotation-driven/>才能启用验证。否则@valid不管用。
如果要使用错误提示的国际化消息,如stu.name为properties文件中的键值对
@Length(min=4,message="{stu.name}")
则需要在dispatcher-servlet.xml中配置
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validmessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
<!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册-->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
<mvc:annotation-driven validator="validator"/>
在src/main/resources下放入validmessages.properties即可。
上面的配置设置了自定义validator,使用messageSource为错误消息提示资源文件。
validmessages.properties内容为
stu.name=\u540D\u79F0\u7684\u957F\u5EA6\u4E0D\u80FD\u5C0F\u4E8E{min}\u554A!
stu.age=\u5E74\u9F84\u5FC5\u987B\u5927\u4E8E{value}\u5C81\u554A!
可以通过{value} {min} {max}等引用注解里的值。
当名称长度小于4 age小于18 输出:
名称的长度不能小于4啊!
age年龄必须大于18岁啊!
本次测试的dispatcher-servlet.xml为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<context:component-scan base-package="com.upper"/>
<!-- freemarker的配置 -->
<bean id="freemarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="defaultEncoding" value="GBK" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
<!-- <prop key="auto_import">c_index.tpl as p</prop> -->
</props>
</property>
</bean>
<!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=GBK" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="requestContextAttribute" value="rc" />
<property name="allowSessionOverride" value="true"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validmessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
<!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册-->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
<mvc:annotation-driven validator="validator"/>
</beans>
http://www.cnblogs.com/beenupper/p/3395872.html
对于后端的参数校验,我们一直在强调的验证规则,提示信息的重用。这不,springmvc通过集成Valid最大程序减少了我们的工作量。其实后端的参数过滤,是分几种请求来源的。每种的处理都不太一样,但是我们如果能重用验证规则,提示信息,那就很强大了。
1 常用的表单提交,需要页面返回错误信息
2 AJAX提交,需要JSON格式返回,或者XML
3 接口调用,同样需要对应的数据格式返回
对于这3类请求,我今天讲的是第3种,是可以重用第1种的资源和验证规则。
考虑通过AOP加注解,拦截方法中的BEAN,通过获取期验证返回信息,提前抛出验证异常。


里面的processValidationError方法会处理具体异常的返回值并以JSON输出,大功告成.
整体代码链接。
demo代码:https://github.com/igool/validatedemo
子模块:https://github.com/igool/lombakcode
当这样处理之后,我们的resetful的接口只用处理真正的业务,完全不用做常用的参数检查。
http://www.blogjava.net/zuxiong/archive/2015/11/27/428389.html
@Valid springMVC bean校验不起作用及如何统一处理校验
标签:
原文地址:http://www.cnblogs.com/softidea/p/5839327.html