声明式数据验证
Posted sunxiaoyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了声明式数据验证相关的知识,希望对你有一定的参考价值。
1.
2. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <!-- 配置前端过滤器 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 表示容器再启动的时候立即记载Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.springmvc-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 采用组件扫描的形式 --> <context:component-scan base-package="com.inspur.controller"></context:component-scan> <mvc:annotation-driven validator="validator"></mvc:annotation-driven> <!-- 配置validator --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> <property name="validationMessageSource" ref="messageSource"></property> </bean> <!-- 配置的messageSource --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" > <property name="basenames" value="classpath:message"></property> <!-- 资源文件的编码格式 --> <property name="fileEncodings" value="utf-8"></property> <!-- 对资源文件内容缓存时间,单位秒 --> <property name="cacheSeconds" value="120"></property> </bean> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="ViewClass" value="org.springframework.web.servlet.view.JstlView"></property> <!-- <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> --> </bean> </beans>
4.ValidateController.java
package com.inspur.controller; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.inspur.pojo.User; @Controller public class ValidateController { @RequestMapping("/validate") public String annotationValidate(@Valid @ModelAttribute("user")User user,Errors errors){ //是否存在错误 if(errors.hasErrors()){ return "/jsp/error.jsp"; } return "/jsp/success.jsp"; } }
5.User.java
package com.inspur.pojo; import javax.validation.constraints.NotNull; public class User { @NotNull(message="{username.not.empty}") private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
6.message.properties
7.error.jsp
8.success.jap
以上是关于声明式数据验证的主要内容,如果未能解决你的问题,请参考以下文章
5. Bean Validation声明式验证四大级别:字段属性容器元素类