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项目。

  • 相关阅读:
    ThreadLocal Thread ThreadLocalMap 之间的关系
    组合注解(Annotation)
    Halo 开源项目使用jsdelivr进行CDN加速.
    如何正确认知和提升自己的“格局”“视野”“高度”?
    格局如何提高?站在未来看今天。
    Cookie 的技术学习笔记
    JVM垃圾收集器与内存分配策略
    java内存区域与内存溢出异常
    JAVA中的BIO,NIO与多路复用(select,poll,epoll)
    Hexo yilia主题搭建自己的github个人博客(一)
  • 原文地址:https://www.cnblogs.com/UalBlog/p/10959682.html
Copyright © 2011-2022 走看看