温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在SpringMVC自定义绑定参数

发布时间:2021-04-17 16:29:21 来源:亿速云 阅读:125 作者:Leah 栏目:编程语言

这篇文章给大家介绍怎么在SpringMVC自定义绑定参数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、概述

1.3 参数绑定过程

怎么在SpringMVC自定义绑定参数

1.2 @RequestParam

如果request请求的参数名和controller方法的形参数名称一致,适配器自动进行参数绑定。如果不一致可以通过 @RequestParam 指定request请求的参数名绑定到哪个方法形参上。

对于必须要传的参数,通过@RequestParam中属性required设置为true,如果不传此参数则报错。

对于有些参数如果不传入,还需要设置默认值,使用@RequestParam中属性defaultvalue设置默认值。

可以绑定简单类型:整型、字符串、单精/双精度、日期、布尔型。

可以绑定简单pojo类型

  • 简单pojo类型只包括简单类型的属性。

  • 绑定过程:request请求的参数名称和pojo的属性名一致,就可以绑定成功。

问题:

  • 如果controller方法形参中有多个pojo且pojo中有重复的属性,使用简单pojo绑定无法有针对性的绑定,

  • 比如:方法形参有items和User,pojo同时存在name属性,从http请求过程的name无法有针对性的绑定到items或user。

二、自定义绑定使用属性编辑器

springmvc没有提供默认的对日期类型的绑定,需要自定义日期类型的绑定。

2.1 使用WebDataBinder(了解)

在controller类中定义:

//自定义属性编辑器
// @InitBinder
// public void initBinder(WebDataBinder binder) throws Exception {
//   // Date.class必须是与controler方法形参pojo属性一致的date类型,这里是java.util.Date
//   binder.registerCustomEditor(Date.class, new CustomDateEditor(
//       new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"), true));
// }

使用这种方法问题是无法在多个controller共用。

2.2 使用WebBindingInitializer(了解)

使用WebBindingInitializer让多个controller共用 属性编辑器。

自定义WebBindingInitializer,注入到处理器适配器中。

如果想多个controller需要共同注册相同的属性编辑器,可以实现PropertyEditorRegistrar接口,并注入webBindingInitializer中。

public class CustomPropertyEditor implements PropertyEditorRegistrar {

  @Override
  public void registerCustomEditors(PropertyEditorRegistry binder) {
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
        new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"), true));
  }

}

配置如下:

<!-- 注册属性编辑器 -->
  <!-- 注册属性编辑器 -->
  <bean id="customPropertyEditor"
class="com.hao.ssm.controller.propertyeditor.CustomPropertyEditor"></bean>

<!-- 自定义webBinder -->
<bean id="customBinder"
  class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
  <property name="propertyEditorRegistrars">
    <list>
      <ref bean="customPropertyEditor"/>
    </list>
  </property>
</bean>

<!--注解适配器 -->
<bean
  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
   <property name="webBindingInitializer" ref="customBinder"></property> 
</bean>

三、自定义参数绑定使用转换器

3.1 实现Converter接口

定义日期类型转换器和字符串去除前后空格转换器。

public class CustomDateConverter implements Converter<String, Date> {

  @Override
  public Date convert(String source) {
    
    try {
      //进行日期转换
      return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
      
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    return null;
  }

}
public class StringTrimConverter implements Converter<String, String> {

  @Override
  public String convert(String source) {
    
    try {
      //去掉字符串两边空格,如果去除后为空设置为null
      if(source!=null){
        source = source.trim();
        if(source.equals("")){
          return null;
        }
      }
      
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    return source;
  }
}

3.2 配置转换器

  <!-- 转换器 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  <property name="converters">
    <list>
      <bean class="com.hao.ssm.controller.converter.CustomDateConverter" />
      <bean class="com.hao.ssm.controller.converter.StringTrimConverter" />
    </list>
  </property>
</bean>

关于怎么在SpringMVC自定义绑定参数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI