1. 程式人生 > >聲明式數據驗證

聲明式數據驗證

font package In AI load welcom 格式 second ber

1.

技術分享圖片

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name
></display-name> <servlet> <!-- 配置前端過濾器 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</
param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 表示容器再啟動的時候立即記載Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</
servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

3.springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


    <!-- 采用組件掃描的形式 -->
    <context:component-scan base-package="com.inspur.controller"></context:component-scan>
    <mvc:annotation-driven validator="validator"></mvc:annotation-driven>
    <!-- 配置validator -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
    
        <property name="validationMessageSource" ref="messageSource"></property>
    </bean>
    <!-- 配置的messageSource -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" >
        <property name="basenames" value="classpath:message"></property>
        <!-- 資源文件的編碼格式 -->
        <property name="fileEncodings" value="utf-8"></property>
        <!-- 對資源文件內容緩存時間,單位秒 -->
        <property name="cacheSeconds" value="120"></property>
    </bean>
    
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="ViewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <!-- <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property> -->
    </bean>
</beans>

4.ValidateController.java

package com.inspur.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.inspur.pojo.User;
@Controller
public class ValidateController {
    @RequestMapping("/validate")
    public String annotationValidate(@Valid @ModelAttribute("user")User user,Errors errors){
        //是否存在錯誤
        if(errors.hasErrors()){
            return "/jsp/error.jsp";
        }
        return "/jsp/success.jsp";
    }

}

5.User.java

package com.inspur.pojo;

import javax.validation.constraints.NotNull;

public class User {
    @NotNull(message="{username.not.empty}")
    private String username;

    public String getUsername() {
        return username;
    }

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

}

6.message.properties

技術分享圖片

7.error.jsp

8.success.jap

聲明式數據驗證