參數綁定
我們在Controller使用方法參數接收值,就是把web端的值給接收到Controller中處理,這個過程就叫做參數綁定…
默認支持的參數類型
從上面的用法我們可以發現,我們可以使用request對象、Model對象等等,其實是不是可以隨便把參數寫上去都行???其實並不是的…
Controller方法默認支持的參數類型有4個,這4個足以支撐我們的日常開發了
- HttpServletRequest
- HttpServletResponse
- HttpSession
- Model
參數的綁定過程
一般地,我們要用到自定義的參數綁定就是上面所講的日期類型轉換以及一些特殊的需求….對於平常的參數綁定,我們是無需使用轉換器的,SpringMVC就已經幫我們幹了這個活了…
自定義綁定參數【版本一】
在上一篇我們已經簡單介紹了怎麼把字符串轉換成日期類型了【使用的是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) {
}