zoukankan      html  css  js  c++  java
  • SpringMVC中的自定义参数绑定案例

      由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适配,并对方法中的形参进行参数绑定。在springmvc这可以在处理器适配器上自定义Converter进行参数绑定。如果使用<mvc:annotation-driven/>可以在此标签上进行扩展。

      步骤:自定义Converter

      

    public class DateConverter implements Converter<String, Date> {

     //String (原数据的类型),Date(转换后的数据类型)

    @Override

     

    public Date convert(String source) {

     

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

     

    try {

     

    return simpleDateFormat.parse(source);

     

    } catch (ParseException e) {

     

    e.printStackTrace();

     

    }

     

    return null;

     

    }

     

    }

     

                  配置Converter(在springmvc的主配置文件中配置)

          

    <!-- 加载注解驱动 -->

    <mvc:annotation-driven conversion-service="conversionService"/>

    <!-- 转换器配置 -->

    <bean id="conversionService"

    class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

    <property name="converters">

    <set>  

    <bean class="cn.kingdee.springmvc.convert.DateConverter"/>

    </set>

    </property>

    </bean>

              配置方式2(了解)

        

    <?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:context="http://www.springframework.org/schema/context"

     

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

     

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

     

            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

     

            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

     

            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

     

     

    <!-- 扫描带Controller注解的类 -->

     

    <context:component-scan base-package="cn.itcast.springmvc.controller" />

     

     

    <!-- 转换器配置 -->

     

    <bean id="conversionService"

     

    class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

     

    <property name="converters">

     

    <set>

     

    <bean class="cn.kingdee.springmvc.convert.DateConverter"/>

     

    </set>

     

    </property>

     

    </bean>

     

    <!-- 自定义webBinder -->

     

    <bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

     

    <property name="conversionService" ref="conversionService" />

     

    </bean>

     

    <!--处理器适配器 -->

     

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

     

     <property name="webBindingInitializer" ref="customBinder"></property> 

     

    </bean>

     

    <!-- 注解处理器映射器 -->

     

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

     

    <!-- 加载注解驱动 -->

     

    <!-- <mvc:annotation-driven/> -->

     

    <!-- 视图解析器 -->

     

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

     

    <property name="viewClass"

     

    value="org.springframework.web.servlet.view.JstlView" />

     

    <!-- jsp前缀 -->

     

    <property name="prefix" value="/WEB-INF/jsp/" />

     

    <!-- jsp后缀 -->

     

    <property name="suffix" value=".jsp" />

     

    </bean>

     

    </beans>

  • 相关阅读:
    二级评论
    AFN上传数组
    简单播放音频
    好久不见~~ iOS开发动画(Animation)总结
    iOS开发中的单元测试(一)
    在 Xcode 中进行自动化测试 (2/2)
    在 Xcode 中进行自动化测试 (1/2)
    iOS中Bitcode的介绍及配置
    大牛博客
    关于iOS Push Notification的响应问题
  • 原文地址:https://www.cnblogs.com/sjzxs/p/9498625.html
Copyright © 2011-2022 走看看