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






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5136793.html,如需转载请自行联系原作者

相关文章
|
21天前
|
Java API 数据安全/隐私保护
掌握Spring Boot中的@Validated注解
【4月更文挑战第23天】在 Spring Boot 开发中,@Validated 注解是用于开启和利用 Spring 的验证框架的一种方式,特别是在处理控制层的输入验证时。本篇技术博客将详细介绍 @Validated 注解的概念和使用方法,并通过实际的应用示例来展示如何在项目中实现有效的数据验证
26 3
|
22天前
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
78 1
|
22天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
30 4
|
23天前
|
Java 开发者 Spring
深入理解 Spring Boot 中的 @EnableAutoConfiguration 注解:概念与实践
【4月更文挑战第21天】在 Spring Boot 项目中,@EnableAutoConfiguration 注解是实现自动配置的核心,它可以根据项目的依赖和配置,自动地配置 Spring 应用程序中的 Bean
34 3
|
24天前
|
Java API 网络架构
深入理解 Spring Boot 中的 @RestController 注解:概念与实践
【4月更文挑战第20天】在现代Web开发中,创建RESTful服务已成为常态。Spring Boot通过提供@RestController注解,极大简化了REST API的开发过程。本篇博客旨在详细介绍@RestController的概念、优势以及在Spring Boot项目中的具体应用方法。
32 8
|
24天前
|
XML Java 数据库
探索 Spring Boot 中的 @Configuration 注解:核心概念与应用
【4月更文挑战第20天】在 Spring Boot 项目中,@Configuration 注解扮演了一个关键角色,它标识一个类作为配置源,这些配置用于定义和管理 Spring 应用程序中的 Bean
44 7
|
19天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
8天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
4天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解
|
6天前
|
前端开发 Java 应用服务中间件
Spring MVC框架概述
Spring MVC 是一个基于Java的轻量级Web框架,采用MVC设计模型实现请求驱动的松耦合应用开发。框架包括DispatcherServlet、HandlerMapping、Handler、HandlerAdapter、ViewResolver核心组件。DispatcherServlet协调这些组件处理HTTP请求和响应,Controller处理业务逻辑,Model封装数据,View负责渲染。通过注解@Controller、@RequestMapping等简化开发,支持RESTful请求。Spring MVC具有清晰的角色分配、Spring框架集成、多种视图技术支持以及异常处理等优点。
13 1