Spring的校验(Validator)
使用Spring校验的大体流程:
最首先要有配置文件xml的支持(spring_validate.xml).(当然在web.xml中要有对该xml的体现)
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
"> <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> <bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 以发布到服务器的路径为准 -->
<property name="basename" value="classpath:messages" />
<property name="fileEncodings" value="utf-8" />
<property name="cacheSeconds" value="120" />
</bean> <bean id="webBindingInitializer"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService">
<bean
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean> </property>
<property name="validator" ref="validator" />
</bean> </beans>
然后就是这个东西登场了:messages.properties.


然后在model层里相应的地方也要有相应的体现
public class User{
@NotEmpty
private String username;
@NotEmpty
private String password;
@NotEmpty
private String repassword;
@NotEmpty
@Email
private String email;
@NotEmpty
private String xm;
}
再然后是control层里的调用:
public String xx(@Valid User user,
BindingResult result, Map map ){ if(result.hasErrors()){
return "xx.ftl";
}
}
最后是在JSP页面了(个人用的是ftl)
首先要有这两句话:Spring的组件
<#assign spring=JspTaglibs["http://www.springframework.org/tags"]/>
<#assign form=JspTaglibs["http://www.springframework.org/tags/form"] />
然后在对应的input下都要有所体现
用户名:<input type="text" name="username" id="username">
<@form.errors path="username"/>
密码:<input type="text" name="password" id="password">
<@form.errors path="password"/>


在最后注意form表单要这样写。其中commandName是那个的实体类名的小写。
<@form.form commandName="user" action="" method="post" name="" id="">
显示的效果是这样:


本帖纯属个人的学习和理解,如有错误请各位大神指正。
Spring的校验(Validator)的更多相关文章
- spring mvc 4数据校验 validator
注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC http://jinnianshilongnian.iteye.com/blog/1733708Spring4新特性——集成B ...
- spring mvc后端校验validator
springmvc中我们使用hibernate的校验框架validation: pom: <validator.version>5.2.4.Final</validator.vers ...
- SpringMVC使用校验validator校验对象属性
1.pom.xm添加依赖 <dependency> <groupId>javax.validation</groupId> <artifactId>va ...
- spring boot:使用validator做接口的参数、表单、类中多字段的参数验证(spring boot 2.3.1)
一,为什么要做参数验证? 永远不要相信我们在后端接收到的数据, 1,防止别人通过接口乱刷服务:有些不怀好意的人或机构会乱刷我们的服务,例如:短信接口, 相信大家可能很多人在工作中遇到过这种情况 2,防 ...
- spring 数据校验之Hibernate validation
1.需要的jar包 2.springsevlet-config.xml配置 在spring3之后,任何支持JSR303的validator(如Hibernate Validator)都可以通过简单配置 ...
- 数据校验validator 与 DWZ
在做系统时经常会用到数据校验,数据校验可以自己写,也可以用现在成的,现在记录下两种类库使用方法, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...
- Spring 捕捉校验参数异常并统一处理
使用 @Validated ,@Valid ,@NotBlank 之类的,请自行百度,本文着重与捕捉校验失败信息并封装返回出去 参考: https://mp.weixin.qq.com/s/EaZxY ...
- spring 接口校验参数(自定义注解)
1. 注解类 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.l ...
- spring 参数校验
1.了解下资源文件加载 MessageSource 需要国际化处理时使用这个类 (在需要处理国际化的地方使用messageSource.getMessage(this.getResponseCod ...
随机推荐
- PHP的date 函数
<!DOCTYPE html> <html> <body> <?php echo "今天是 " . date("Y/m/d&qu ...
- SDUT OJ 2054 双向链表的实现 (结构体node指针+遍历 *【模板】)
双向链表 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内 ...
- Ubuntu安装mycli,让mysql命令行可以自动提示
安装mycli 1.确保有安装python 2.确保有安装pip 3.进入su模式,以管理员身份安装 4.安装 pip install -U mycli 5.登录 mycli -u root 很好很强 ...
- html5--6-4 CSS选择器
html5--6-4 CSS选择器 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作用 ...
- fastText(三):微博短文本下fastText的应用(二)
上一篇讲到,fastText在训练数据中过拟合的问题.接下来将介绍一些提高fastText泛化能力的尝试. 模型泛化使用过fastText的人,往往会被它的很多特性征服,例如训练速度.兼具word e ...
- absolute 和float
position:absolute与float:left是近亲 包裹性 包裹性换种说法就是让元素inline-block化,例如一个div标签默认宽度是100%显示的,但是一旦被absolute属性缠 ...
- GC 和 Full GC 有什么区别?
GC(或Minor GC):收集 生命周期短的区域(Young area). Full GC (或Major GC):收集生命周期短的区域(Young area)和生命周期比较长的区域(Old are ...
- 清理c盘的文件
C:/Users/用户名/AppData里面默认有三个文件夹,分别是Local,LocalLow,Roaming,简单地来说,都是用来存放软件的配置文件和临时文件的,里面有很多以软件公司或者软件名称命 ...
- git中文件的三种状态
用xcode的时候,左侧栏文件的邮右边时不时会看到M,A这一类的字母.当然,这些以后再写上.先说一下git里文件的三种状态 已提交(committed) 已经提交的本地仓库(repository), ...
- Java简单高精度合集
第一个Java的算法程序.记得可以使用Alt+'/'自动补全sysout和main之类的. BigInteger在java.math.BigInteger中. import java.math.Big ...