一、概述

1.3 参数绑定过程


153805

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】参数绑定的更多相关文章

  1. 一篇文章搞定SpringMVC参数绑定

    SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了 下面通过5个常用的注解演示下如何进行 ...

  2. [转载]SpringBoot系列: SpringMVC 参数绑定注解解析

    本文转载自 https://www.cnblogs.com/morethink/p/8028664.html, 作者写得非常好, 致谢! SpringMVC 参数绑定注解解析   本文介绍了用于参数绑 ...

  3. SpringMVC参数绑定,这篇就够了!

    SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了 下面通过5个常用的注解演示下如何进行 ...

  4. SpringMvc参数绑定出现乱码解决方法

    在SpringMvc参数绑定过程中出现乱码的解决方法 1.post参数乱码的解决方法 在web.xml中添加过滤器 <!-- 过滤器 处理post乱码 --> <filter> ...

  5. SpringMVC参数绑定(未完待续)

    1. Strut2与SpringMVC接收请求参数的区别 Struts2通过action类的成员变量接收SpringMVC通过controller方法的形参接收 2. SpringMVC参数绑定流程 ...

  6. SpringMVC参数绑定(从请求中接受参数)

    参数绑定(从请求中接收参数) 1)默认类型: 在controller方法中可以有也可以没有,看自己需求随意添加. httpservletRqeust,httpServletResponse,httpS ...

  7. SpringMVC 参数绑定注解解析

    本文介绍了用于参数绑定的相关注解. 绑定:将请求中的字段按照名字匹配的原则填入模型对象. SpringMVC就跟Struts2一样,通过拦截器进行参数匹配. 代码在 https://github.co ...

  8. SpringMVC参数绑定总结

    springMvc作用:    a) 接收请求中的参数    b) 将处理好的数据返回给页面参数绑定(就是从请求中接收参数):    a) 默认支持的类型: request, response, se ...

  9. SpringMVC参数绑定(四)

    1.默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值. HttpServletRequest 通过request对象获取请求信息 HttpServletResponse ...

  10. SpringMVC参数绑定、Post乱码解决方法

    从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上. springmvc中,接收页面提交的数据是通过方法形参来接收.而不是在control ...

随机推荐

  1. Windows下MariaDB数据库安装图文教程

    MariaDB是基于MySQL的开源数据库,兼容MySQL,现有的MySQL数据库可以迁移到MariaDB中使用   说明: MariaDB是基于MySQL的开源数据库,兼容MySQL,现有的MySQ ...

  2. 如何让winrar5压缩的文件能用低版本winrar打开

    https://jingyan.baidu.com/article/39810a2348ab24b636fda681.html 在压缩文件格式选项处点选[RAR4]选项,即之前版本的winrar支持的 ...

  3. Java合并(连接)多个音频

    java sound resource 合并的说法有歧义,为了方便大家搜索到这里,所以用这个标题,实际上是连接(concat),可以理解为字符串concat方法所指定的含义. AudioConcat. ...

  4. [转载] HashMap的工作原理-hashcode和equals的区别

    目录 前言 为什么需要使用Hashcode,可以从Java集合的常用需求来描述: 更深入的介绍 先来些简单的问题 HashMap的0.75负载因子 总结 我在网上看到的这篇文章,介绍的很不错,但是我看 ...

  5. [ jenkins ] git+jenkins+maven + ansible 部署java程序

    1. 工具介绍 git:版本控制,一般都是托管到代码仓库的.如 github.coding.gitlab (本文以 coding 为例) jenkins:持续集成工具之一,也是最常用的工具,主要工作就 ...

  6. require.js的基本用法

    一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...

  7. mysql索引 多个单列索引和联合索引的区别详解

    背景: 为了提高数据库效率,建索引是家常便饭:那么当查询条件为2个及以上时,我们是创建多个单列索引还是创建一个联合索引好呢?他们之间的区别是什么?哪个效率高呢?我在这里详细测试分析下. 一.联合索引测 ...

  8. centos7 install docker

    sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo y ...

  9. string中的erase()函数

    erase()是对string类型的字符串进行删除元素操作的函数 1.erase(int index) 删除下标从index开始直到字符串结尾的元素 1 string s = "123215 ...

  10. 031 Android 自定义控件

    1.自定义控件的优点 Android自身带的控件不能满足需求, 需要根据自己的需求定义控件. 2.自定义控件的分类: (1)组合已有的控件实现 (2)继承已有的控件实现(扩展已有的功能) (3)完全自 ...