咱们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫作参数绑定…php
从上面的用法咱们能够发现,咱们可使用request对象、Model对象等等,实际上是不是能够随便把参数写上去都行???其实并非的…css
Controller方法默认支持的参数类型有4个,这4个足以支撑咱们的平常开发了java
通常地,咱们要用到自定义的参数绑定就是上面所讲的日期类型转换以及一些特殊的需求….对于日常的参数绑定,咱们是无需使用转换器的,SpringMVC就已经帮咱们干了这个活了…web
在上一篇咱们已经简单介绍了怎么把字符串转换成日期类型了【使用的是WebDataBinder方式】…其实那是一个比较老的方法,咱们可使用SpringMVC更推荐的方式…spring
在上次把字符串转换成日期类型,若是使用的是WebDataBinder方式的话,那么该转换仅仅只能在当前Controller使用…若是想要所有的Controller都可以使用,那么咱们可使用WebBindingInitializer方式markdown
若是想多个controller须要共同注册相同的属性编辑器,能够实现PropertyEditorRegistrar接口,并注入webBindingInitializer中。mvc
实现接口app
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比上面有什么好ide
配置日期转换器
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>
咱们通常使用的参数绑定都有遵循的规则:方法参数名要与传递过来的name属性名相同。
在默认的状况下,只有名字相同,SpringMVC才会帮咱们进行参数绑定…
若是咱们使用@RequestParam注解
的话,咱们就可使方法参数名与传递过来的name属性名不一样…
该注解有三个变量
例子:咱们的方法参数叫id,而页面带过来的name属性名字叫item_id,必定须要该参数
public String editItem(@RequestParam(value="item_id",required=true) String id) {
}