zoukankan      html  css  js  c++  java
  • SpringMVC的自定义校验器

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basenames" value="classpath:messages"/>
            <!--指定文件的编码-->
            <property name="fileEncodings" value="utf8"/>
            <!--对资源文件的缓存时间-->
            <property name="cacheSeconds" value="120"/>
        </bean>

    以上使spring-web.xml中的配置,其中,由于messages是放在src下的resource文件夹中,所以需要使用classpath作为定位。

    package com.ual.web;
    
    import com.ual.model.Employee;
    import org.springframework.validation.Errors;
    import org.springframework.validation.ValidationUtils;
    import org.springframework.validation.Validator;
    
    public class EmployeeValidator implements Validator {
        @Override
        public boolean supports(Class<?> aClass) {
            return Employee.class.isAssignableFrom(aClass);
        }
    
        @Override
        public void validate(Object o, Errors errors) {
            Employee employee=(Employee)o;
            ValidationUtils.rejectIfEmpty(errors,"name","name.required");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors,"birthDate","birthDate.required"); } }
     @Override
        public void validate(Object o, Errors errors) {
            Employee employee=(Employee)o;
            ValidationUtils.rejectIfEmpty(errors,"name","employee.name.required");
            if(employee.getName()!=null&&employee.getName().equals("123")){
                errors.rejectValue("name","employee.name.NotOne");
            }
           ValidationUtils.rejectIfEmpty(errors,"birthDate","employee.birthDate.required");
        }

    以上是自定义的validator。

    视图中:

    <%@page language="java"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Getting Started:Serving Web Content</title>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    </head>
    <body>
    <div id="global">
        <fieldset>
        <form:form commandName="employee" action="save-employee" method="post">
            <legend> Add an employee</legend>
            <p>
                <form:errors path="name" />
            </p>
            <p>
                <label >Name:</label>
                <form:input path="name" tabindex="1"/>
            </p>
            <p>
                <form:errors path="birthDate" cssClass="error"/>
            </p>
            <p>
                <label>BirthDate</label>
                <form:input path="birthDate" tabindex="2"/>
            </p>
            <p id="buttons">
                <input id="reset" type="reset" tabindex="3"/>
                <input id="submit" type="submit" tabindex="4" value="AddEmployee"/>
    
            </p>
        </fieldset>
        </form:form>
    </div>
    </body>
    </html>

     此代码 基于dianzan项目。

  • 相关阅读:
    利用相关的Aware接口
    java 值传递和引用传递。
    权限控制框架Spring Security 和Shiro 的总结
    优秀代码养成
    Servlet 基础知识
    leetcode 501. Find Mode in Binary Search Tree
    leetcode 530. Minimum Absolute Difference in BST
    leetcode 543. Diameter of Binary Tree
    leetcode 551. Student Attendance Record I
    leetcode 563. Binary Tree Tilt
  • 原文地址:https://www.cnblogs.com/UalBlog/p/10959682.html
Copyright © 2011-2022 走看看