• spring mvc 参数类型转换


    实现方式以字符串转Date为例说明:

    全局配置

    第一种:实现 Converter 接口

    • 实现类: 
      public class StringToDateConveter implements Converter {

          private String formatPatten;
      
          public StringToDateConveter(String formatPatten){
              this.formatPatten=formatPatten;
          }
      
          @Override
          public Date convert(String s) {
              return DateUtil.string2Date(s,formatPatten);
          }
      }
      
    • mvc.xml配置

      <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
      
      <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
          <property name="converters">
              <set>
                  <bean class="com.lannong.api.www.converter.StringToDateConveter">
                      <constructor-arg name="formatPatten" value="yyyy-MM-dd"/>
                  </bean>
              </set>
          </property>
      </bean>
      
    • 配置到handlerAdapter

         <!--使用 ConfigurableWebBindingInitializer 注册conversionService-->
         <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
              <property name="conversionService" ref="conversionService"/>
         </bean>
      
         <!-- 注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter-->
         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
              <property name="webBindingInitializer" ref="webBindingInitializer"/>
         </bean>
      

    第二种:实现Formatter接口,与第一种实现方式类似

    • 实现类

      public class MyDateFormater implements Formatter<Date> {
      
          @Override
          public Date parse(String s, Locale locale) throws ParseException {
              return DateTimeUtil.string2Date(s,"yyyy-MM-dd");
          }
      
          @Override
          public String print(Date date, Locale locale) {
              return null;
          }
      }
      
    • mvc.xml配置

      <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
      
      <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
          <property name="formatters">
              <set>
                  <bean class="com.lannong.api.www.converter.MyDateFormater"/>
              </set>
          </property>
      </bean>
      

    第三种:实现WebBindingInitializer接口

    • 实现类

      public class MyWebBindingInitializer implements WebBindingInitializer {
      
          @Override
          public void initBinder(WebDataBinder binder, WebRequest request) {
      
              binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
                  @Override
                  public void setAsText(String text) {
                      setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
                  }
              });
          }
      }
      
    • mvc.xml配置

      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
          <property name="webBindingInitializer">
              <bean class="com.lannong.api.www.binder.MyWebBindingInitializer"/>
          </property>
          <!-- others config -->
      </bean>
      

    局部配置

    在Controller中添加转换方法并添加@InitBinder

    • 代码

      @InitBinder
      public void initBinder(WebDataBinder webDataBinder) throws Exception{
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
          simpleDateFormat.setLenient(false);
          webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
      }
      
      或
      
      @InitBinder
      public void initBinder(WebDataBinder binder, WebRequest request) {
          binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
              @Override
              public void setAsText(String text) {
                  setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
              }
          });
      }
      

    两种方式都可以,作用域和该方法作用域一样

    使用@DateTimeFormat(pattern = "yyyy-MM-dd")

    注解可以加在属性上,也可以加在方法上,需要导入joda-time.jar。另外日期参数的格式需要和patten定义的一致,否则会报400错误

  • 相关阅读:
    Nginx Rewrite详解
    linux下面mysql的基本命令
    通过淘宝的rubygems镜像,安装rubygems
    PHP 调用函数时,函数名称前面加@的作用
    mysql5.5中 host的值为::1
    CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13+博客系统WordPress3.3.2
    ruby数组方法concat和push的区别
    PHP 的时间格式
    EOF与getchar
    控件台版/MFC版本的简单生日提醒软件及源码
  • 原文地址:https://www.cnblogs.com/f-anything/p/8707495.html
Copyright © 2020-2023  润新知