Spring MVC 数据验证——validate注解方式

1、说明

学习注解方式之前,应该先学习一下编码方式的spring注入。这样便于理解验证框架的工作原理。在出错的时候,也能更好的解决问题。所以本次博客教程也是基于编码方式,只是在原来的基础加上注解方式。

2、配置信息

  • web.xml不需要改变的
  • hello-servlet.xml将原来的加载方式,改为自动加入有hibernate和Spring提供的validate的默认类,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan 
    base-package="controller">
    </context:component-scan> 
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
        <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> -->
        </bean>

<!--基于validate的加载配置-->
   <mvc:annotation-driven validator="validator"/>

 <!--    <bean id="accountValidator" class="dao.AccountValidator"></bean> -->

    <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:message"/>
            <property name= "fileEncodings" value= "utf-8"/>
            <property name= "cacheSeconds" value= "120"/>
    </bean>


</beans>
  • 在src目录下面新建配置文件message.properties 给文件的加载是由hello-servlet.xml中的字段messageSource中的属性basename的value值决定的。可以不用配置这个文件,但是一般为了支持国际化,但是吧信息写到配置文件中的,如下:
account.username.size=\u7528\u6237\u540D\u7684\u957F\u5EA6\u57283-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4
account.username.space=\u7528\u6237\u540D\u5B57\u7B26\u4E32\u4E4B\u95F4\u4E0D\u80FD\u6709\u7A7A\u683C
account.password.size=\u5BC6\u7801\u7684\u957F\u5EA6\u8303\u56F4\u57286-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4

后面的乱码都是转义后生成的,对应的中文如下:
这里写图片描述

3、源代码
- 修改Account类,如下代码:

package dao;

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

public class Account {

    @Size(min=3,max=20,message="{account.username.size}")
    @Pattern(regexp="^[a-zA-Z0-9]+$",message="{account.username.space}")
    private String username;

    @Size(min=6,max=20,message="{account.password.size}")
    private String password;


    public Account() {
        // TODO Auto-generated constructor stub
    }

    public Account(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



}

其中的@Size @Pattern都是注解方式的数据验证方式,后面的message信息都是定义在message.properties中

  • 不需要AccountValidate类了

3、效果演示

启动tomcat服务,在浏览器中输入:http://localhost:8080/annotation_springmvc/register

我们是用一个大有空格字符串测试一下:
这里写图片描述
注册效果:
这里写图片描述

4、代码位置

如果有需要的,可以去这个位置下载下来看看:注解方式的数据验证,jar包也上传了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值