• 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.yxj.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.yxj.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() );
            }
  • 相关阅读:
    DNS 服务器的配置与管理
    为什么苹果不再需要谷歌地图?
    flash安装时提示无法安装解决方法
    苹果新ipad支持siri吗?答案是不支持!
    HTTP的 Basic 验证
    笑解优酷土豆合并
    crontab简介
    循环链表应用
    计算表达式
    走迷宫 dfs
  • 原文地址:https://www.cnblogs.com/yejiaojiao/p/6163647.html
Copyright © 2020-2023  润新知