zoukankan      html  css  js  c++  java
  • Spring3 MVC 类型转换

    1. Spring在进行类型转化都是基于java.beans.PropertyEditor接口。

    2. 可以使用@InitBinder来进行对单个controller的类型进行操作,比如添加Date类型的转换器:

    @InitBinder
        public void initBinder(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(
                    dateFormat, false));
        }

    3. 也可以针对全局的转换器,实现WebBindingInitializer 接口:

    public class CustomerBinding implements WebBindingInitializer {
        @Override
        public void initBinder(WebDataBinder binder, WebRequest request) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(
                    dateFormat, false));
    
        }

    3. <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean

    DispatcherServlet默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml”

    HandlerMapping接口 -- 处理请求的映射

    HandlerMapping接口的实现类:

    SimpleUrlHandlerMapping  通过配置文件,把一个URL映射到Controller

    DefaultAnnotationHandlerMapping  通过注解,把一个URL映射到Controller类上

    HandlerAdapter接口 -- 处理请求的映射

    AnnotationMethodHandlerAdapter类,通过注解,把一个URL映射到Controller类的方法上

    Controller接口 -- 控制器

    由于我们使用了@Controller注解,添加了@Controller注解注解的类就可以担任控制器(Action)的职责,

    所以我们并没有用到这个接口。

  • 相关阅读:
    arduino电子艺术PWM直流电机电调实验
    横坐标轴移动位置
    将不才则三军倾
    Source Insight常用快捷键及注释快捷键设置
    dos2unix批量转换的脚本
    win8: ListView
    win8: Asynchronous Programming in JavaScript with “Promises”
    WindJS 中的$await
    iphone:关于沙盒 存储路径
    win8: hello gril
  • 原文地址:https://www.cnblogs.com/ranger2016/p/3888500.html
Copyright © 2011-2022 走看看