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

  • 相关阅读:
    并行fp-growth图解(mahout)
    Sqoop的安装与使用
    深入理解Hadoop集群和网络
    datanode与namenode的通信原理
    Hadoop添加节点datanode(生产环境)
    Hadoop中HDFS工作原理
    实现hadoop中的机架感知
    hadoop集群监控工具Apache Ambari安装配置教程
    sdn测量综述
    SDN测量论文粗读(三)9.24
  • 原文地址:https://www.cnblogs.com/UalBlog/p/10959682.html
Copyright © 2020-2023  润新知