springmvc的初始化参数绑定

                               springmvc的初始化参数绑定

此种和我们之前说的类型转换非常相似,可以看作是一种类型转换

在初始化参数绑定时  重要的是参数类型

-------------------单日期的绑定

  配置步骤:

  步骤一:在applicationcontext.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:p="http://www.springframework.org/schema/p"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
 
     <!--让spring扫描包下所有的类,让标注spring注解的类生效  -->
     <context:component-scan base-package="cn.hmy.controller"/>
     
</beans>

步骤二:在处理器类中配置绑定方法  使用@InitBinder注解

在这里首先注册一个用户编辑器 参数一为目标类型   propertyEditor为属性编辑器,此处我们选用 CustomDateEditor属性编辑器,

参数一为想转换的日期格式,参数二表示是否允许为空

@InitBinder
        public void databinder(WebDataBinder binder){
            System.out.println("11111");
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class,new CustomDateEditor(df, true) );
        }

单个日期的参数绑定配置完毕

-------------------多日期的绑定

配置步骤:

1.属性编辑器

此时我们需要考虑使用哪个属性编辑器,需要定义自己的属性编辑器

大致的配置方式如单日期相似,只需要更换属性编辑即可

自定义的属性编辑器,需要我们继承PropertiesEditor,重写里面的setAsText方法,使用setValue方法赋值

package cn.hmy.propertyEdit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import org.springframework.beans.propertyeditors.PropertiesEditor;


public class MyDataEdit extends PropertiesEditor{

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        SimpleDateFormat sdf=getDateFormat(text);
        try {
            Date date = sdf.parse(text);
            setValue(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private SimpleDateFormat getDateFormat(String text) {
        if(Pattern.matches("^\d{4}-\d{2}-\d{2}$", text)){
            return new SimpleDateFormat("yyyy-MM-dd");
        }else if(Pattern.matches("^\d{4}/\d{2}/\d{2}$", text)){
            return new SimpleDateFormat("yyyy/MM/dd");
        }else if(Pattern.matches("^\d{4}\d{2}\d{2}$", text)){
            return new SimpleDateFormat("yyyyMMdd");
        }else if(Pattern.matches("^\d{4}年\d{2}月\d{2}日$", text)){
            return new SimpleDateFormat("yyyy年MM月dd日");
        }
        return null;
    }

    
    

}

步骤二:在处理器类中使用我们自定的属性编辑器

@InitBinder
        public void databinder(WebDataBinder binder){
            System.out.println("11111");
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class,new MyDataEdit() );
        }
原文地址:https://www.cnblogs.com/hmy-1365/p/6097912.html