自定义参数绑定

版权声明:转载请注明:beOkWithAnything总结 https://blog.csdn.net/swq463/article/details/81699470

springMVC框架给做了大部分的参数绑定方式,默认支持很多类型:

     HttpServletRequest、HttpServletResponse、session、

     model接口/modelMap实现(将模型数据最终能填充到request域)、

     简单的数据类型:Integer、String、日期型

     pojo类型:保证request提交的参数名和属性名称一致,自动绑定

public void class getUser( Integer userId,User user ){

        if( userId != null ){

        }

}

@RequestParam注解:

     当request请求的参数名和形参名称不一样时,用来指定形参对应的参数名


网页页面上POST提交的的日期是一个字符串,
但在Controller方法(Items items)绑定参数时pojo中的createTime为java.util.Data类型
参数绑定会出错( 貌似是框架默认只能绑定到年月日,没有时分秒 ),所以要用到自定义参数绑定

向处理器适配器中注入自定义的参数绑定组件:

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

<!--自定义参数绑定组件-->
<bean id="conversionService"
      class="org.springframe.format.support.FormattingConversionServiceFactoryBean">
    <!--转换器-->
    <list>
        <!--日期类型的转换-->
        <bean class="cn.yunding.test.util.Converter"/>
        <!--其他类型的转换-->
        <bean class=""/>
    </list>
</bean>

Converter实现类:

public class Converter implements Converter<String,Date>{
    public Date convert(String s){
        SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simp.parse(s);
    }
}

猜你喜欢

转载自blog.csdn.net/swq463/article/details/81699470
今日推荐