zoukankan      html  css  js  c++  java
  • 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=u7528u6237u540Du7684u957Fu5EA6u57283-20u4E2Au5B57u7B26u4E32u4E4Bu95F4
    account.username.space=u7528u6237u540Du5B57u7B26u4E32u4E4Bu95F4u4E0Du80FDu6709u7A7Au683C
    account.password.size=u5BC6u7801u7684u957Fu5EA6u8303u56F4u57286-20u4E2Au5B57u7B26u4E32u4E4Bu95F4
    

    后面的乱码都是转义后生成的。相应的中文例如以下:
    这里写图片描写叙述

    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包也上传了

  • 相关阅读:
    Java中Runnable和Thread的区别
    JAVA Swing 事件监听
    java 监听机制模拟(JButton按钮监听机制)
    java事件处理机制(自定义事件)
    oracle之检查点(Checkpoint)
    linux内核值shmmax问题
    如何在VMware虚拟机间建立共享磁盘?
    Mysql 不同版本 说明
    mysql 概念和逻辑架构
    mysql 在大型应用中的架构演变
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5136793.html
Copyright © 2011-2022 走看看