SpringMVC第四篇【参数绑定详讲、默认支持参数类型、自定义参数绑定、RequestParam注解】...


tags: SpringMVC


SpringMVC第四篇【参数绑定详讲、默认支持参数类型、自定义参数绑定、RequestParam注解】

参数绑定

我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定...

默认支持的参数类型

从上面的用法我们可以发现,我们可以使用request对象、Model对象等等,其实是不是可以随便把参数写上去都行???其实并不是的...

Controller方法默认支持的参数类型有4个,这4个足以支撑我们的日常开发了

  • HttpServletRequest
  • HttpServletResponse
  • HttpSession
  • Model

参数的绑定过程

一般地,我们要用到自定义的参数绑定就是上面所讲的日期类型转换以及一些特殊的需求....对于平常的参数绑定,我们是无需使用转换器的,SpringMVC就已经帮我们干了这个活了...

自定义绑定参数【老方式、全部Action均可使用】

在上一篇我们已经简单介绍了怎么把字符串转换成日期类型了【使用的是WebDataBinder方式】...其实那是一个比较老的方法,我们可以使用SpringMVC更推荐的方式...

在上次把字符串转换成日期类型,如果使用的是WebDataBinder方式的话,那么该转换仅仅只能在当前Controller使用...如果想要全部的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));
		
	}

}
复制代码

配置转换器

注入到webBindingInitializer中

	<!-- 注册属性编辑器 -->
	<bean id="customPropertyEditor" class="cn.itcast.ssm.controller.propertyeditor.CustomPropertyEditor"></bean>
	
	
	<!-- 自定义webBinder -->
	<bean id="customBinder"
		class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
	
		<!-- propertyEditorRegistrars用于属性编辑器 -->
		 <property name="propertyEditorRegistrars">
			<list>
				<ref bean="customPropertyEditor" />
			</list>
		</property>
	</bean>


	<!-- 注解适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<!-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 -->
		<property name="webBindingInitializer" ref="customBinder"></property>
	</bean>


复制代码

自定义参数转换器【新方式、推崇方式】

上面的方式是对象较老的,现在我们一般都是实现Converter接口来实现自定义参数转换...我们就来看看实现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;
	}
}

复制代码

从上面可以得出,我们想要转换什么内容,就直接实现接口,该接口又是支持泛型的,阅读起来就非常方便了...

配置转换器



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


	<!-- 自定义webBinder -->
	<bean id="customBinder"
		class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
		<!-- 使用converter进行参数转 -->
		<property name="conversionService" ref="conversionService" />
	</bean>


	<!-- 注解适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<!-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 -->
		<property name="webBindingInitializer" ref="customBinder"></property>
	</bean>


	
复制代码

如果是基于<mvc:annotation-driven>的话,我们是这样配置的


<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
<!-- conversionService -->
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<!-- 转换器 -->
		<property name="converters">
			<list>
				<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
				<bean class="cn.itcast.ssm.controller.converter.StringTrimConverter"/>
			</list>
		</property>
	</bean>
复制代码

@RequestParam注解

我们一般使用的参数绑定都有遵循的规则:方法参数名要与传递过来的name属性名相同。

在默认的情况下,只有名字相同,SpringMVC才会帮我们进行参数绑定...

如果我们使用@RequestParam注解的话,我们就可以使方法参数名与传递过来的name属性名不同...

该注解有三个变量

  • value【指定name属性的名称是什么】
  • required【是否必须要有该参数】
  • defaultvalue设置默认值

例子:我们的方法参数叫id,而页面带过来的name属性名字叫item_id,一定需要该参数


public String editItem(@RequestParam(value="item_id",required=true) String id) {

}
复制代码

总结

  • 在SpringMVC中的业务方法默认支持的参数有四种

    • request
    • response
    • session
    • model
  • 我们的参数绑定(自动封装参数)是由我们的转换器来进行绑定的。现在用的一般都是Converter转换器

  • 在上一章中我们使用WebDataBinder方式来实现对日期格式的转化,当时仅仅是可用于当前Action的。我们想要让全部Action都可以使用的话,有两种方式:

    • 实现PropertyEditorRegistrar(比较老的方式)
    • 实现Converter(新的方式)
  • 参数绑定都有遵循的规则:方法参数名要与传递过来的name属性名相同

    • 我们可以使用@RequestParam注解来具体指定对应的name属性名称,这样也是可以实现参数绑定的。
    • 还能够配置该参数是否是必须的。

如果您觉得这篇文章帮助到了您,可以给作者一点鼓励

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值