使用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)的更多相关文章

  1. spring mvc 4数据校验 validator

    注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC http://jinnianshilongnian.iteye.com/blog/1733708Spring4新特性——集成B ...

  2. spring mvc后端校验validator

    springmvc中我们使用hibernate的校验框架validation: pom: <validator.version>5.2.4.Final</validator.vers ...

  3. SpringMVC使用校验validator校验对象属性

    1.pom.xm添加依赖 <dependency> <groupId>javax.validation</groupId> <artifactId>va ...

  4. spring boot:使用validator做接口的参数、表单、类中多字段的参数验证(spring boot 2.3.1)

    一,为什么要做参数验证? 永远不要相信我们在后端接收到的数据, 1,防止别人通过接口乱刷服务:有些不怀好意的人或机构会乱刷我们的服务,例如:短信接口, 相信大家可能很多人在工作中遇到过这种情况 2,防 ...

  5. spring 数据校验之Hibernate validation

    1.需要的jar包 2.springsevlet-config.xml配置 在spring3之后,任何支持JSR303的validator(如Hibernate Validator)都可以通过简单配置 ...

  6. 数据校验validator 与 DWZ

    在做系统时经常会用到数据校验,数据校验可以自己写,也可以用现在成的,现在记录下两种类库使用方法, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...

  7. Spring 捕捉校验参数异常并统一处理

    使用 @Validated ,@Valid ,@NotBlank 之类的,请自行百度,本文着重与捕捉校验失败信息并封装返回出去 参考: https://mp.weixin.qq.com/s/EaZxY ...

  8. spring 接口校验参数(自定义注解)

    1. 注解类 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.l ...

  9. spring 参数校验

    1.了解下资源文件加载 MessageSource   需要国际化处理时使用这个类 (在需要处理国际化的地方使用messageSource.getMessage(this.getResponseCod ...

随机推荐

  1. iOS webView的常见属性和方法

    一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种: 第一种: - (void)load ...

  2. golang---信号signal

    golang中os/signal包的使用 chenbaoke · 2015-06-17 20:03:59 · 2748 次点击 · 预计阅读时间 1 分钟 · 不到1分钟之前 开始浏览 这是一个创建于 ...

  3. HTTP服务器用什么组件或者方式比较好

    我目前用Indy的HttpServer组件来编写,但遇到一个暂时没有办法解决的问题,就是上传文件到这个HTTPServer,如果文件名包含中文,则会出现乱码.网上查了一下,这是个indy的遗留问题,据 ...

  4. 秒懂单链表及其反转(reverse)

    什么是链表,这种数据结构是由一组Node组成的,这群Node一起表示了一个序列.链表是最普通,最简单的数据结构(物理地址不连续),它是实现其他数据结构如stack, queue等的基础. 链表比起数组 ...

  5. get all sites under IIS

    https://stackoverflow.com/questions/2555668/how-to-programmatically-get-sites-list-and-virtual-dirs- ...

  6. maven实战(4)-- maven构建自己的jar到本地仓库中

    在平时的开发中,经常需要用到自己以前开发的一个jar包,那么如何使用将自己开发的jar提交到本地仓库中,供其他的项目依赖呢?主要有三种方法: (1)使用mvn的构建命令:mvn clean insta ...

  7. FTOUR2 - Free tour II

    传送门 题目翻译的很清楚……似乎点分治的题题目描述都非常简洁. 还是那个操作,一条路径要么全部在一棵子树中,要么经过当前的重心,所以考虑点分治. 首先dfs求出重心的每一棵子树中,有i个黑点的最长路径 ...

  8. Git如何删除自己创建的项目

    版本管理器第二行最右边,找到倒三角,下面的Edit Project,拖动鼠标到最下面,Remove project ,弹出框Confirmation required里面输入项目名字,如项目名字为“w ...

  9. Makefile的引入及规则

    ARM裸机1期加强版视频课程配套WiKi第9课第5节_Makefile的引入及规则. 文字不能完全替代视频,所以如果你看了这些文章不太懂,建议购买视频进一步学习. 视频购买地址:100ask.taob ...

  10. kafka之四:Kafka集群搭建

    1.软件环境 1.linux一台或多台,大于等于2 2.已经搭建好的zookeeper集群 3.软件版本kafka_2.11-0.9.0.1.tgz 2.创建目录并下载安装软件 #创建目录 cd /o ...