SpringMVC自定義參數(shù)綁定實(shí)現(xiàn)詳解
一、概述
1.3 參數(shù)綁定過(guò)程

1.2 @RequestParam
如果request請(qǐng)求的參數(shù)名和controller方法的形參數(shù)名稱(chēng)一致,適配器自動(dòng)進(jìn)行參數(shù)綁定。如果不一致可以通過(guò) @RequestParam 指定request請(qǐng)求的參數(shù)名綁定到哪個(gè)方法形參上。
對(duì)于必須要傳的參數(shù),通過(guò)@RequestParam中屬性required設(shè)置為true,如果不傳此參數(shù)則報(bào)錯(cuò)。
對(duì)于有些參數(shù)如果不傳入,還需要設(shè)置默認(rèn)值,使用@RequestParam中屬性defaultvalue設(shè)置默認(rèn)值。
可以綁定簡(jiǎn)單類(lèi)型:整型、字符串、單精/雙精度、日期、布爾型。
可以綁定簡(jiǎn)單pojo類(lèi)型
- 簡(jiǎn)單pojo類(lèi)型只包括簡(jiǎn)單類(lèi)型的屬性。
- 綁定過(guò)程:request請(qǐng)求的參數(shù)名稱(chēng)和pojo的屬性名一致,就可以綁定成功。
問(wèn)題:
- 如果controller方法形參中有多個(gè)pojo且pojo中有重復(fù)的屬性,使用簡(jiǎn)單pojo綁定無(wú)法有針對(duì)性的綁定,
- 比如:方法形參有items和User,pojo同時(shí)存在name屬性,從http請(qǐng)求過(guò)程的name無(wú)法有針對(duì)性的綁定到items或user。
二、自定義綁定使用屬性編輯器
springmvc沒(méi)有提供默認(rèn)的對(duì)日期類(lèi)型的綁定,需要自定義日期類(lèi)型的綁定。
2.1 使用WebDataBinder(了解)
在controller類(lèi)中定義:
//自定義屬性編輯器
// @InitBinder
// public void initBinder(WebDataBinder binder) throws Exception {
// // Date.class必須是與controler方法形參pojo屬性一致的date類(lèi)型,這里是java.util.Date
// binder.registerCustomEditor(Date.class, new CustomDateEditor(
// new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"), true));
// }
使用這種方法問(wèn)題是無(wú)法在多個(gè)controller共用。
2.2 使用WebBindingInitializer(了解)
使用WebBindingInitializer讓多個(gè)controller共用 屬性編輯器。
自定義WebBindingInitializer,注入到處理器適配器中。
如果想多個(gè)controller需要共同注冊(cè)相同的屬性編輯器,可以實(shí)現(xiàn)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));
}
}
配置如下:
<!-- 注冊(cè)屬性編輯器 -->
<!-- 注冊(cè)屬性編輯器 -->
<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>
三、自定義參數(shù)綁定使用轉(zhuǎn)換器
3.1 實(shí)現(xiàn)Converter接口
定義日期類(lèi)型轉(zhuǎn)換器和字符串去除前后空格轉(zhuǎn)換器。
public class CustomDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
//進(jìn)行日期轉(zhuǎn)換
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 {
//去掉字符串兩邊空格,如果去除后為空設(shè)置為null
if(source!=null){
source = source.trim();
if(source.equals("")){
return null;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return source;
}
}
3.2 配置轉(zhuǎn)換器
<!-- 轉(zhuǎn)換器 -->
<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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解springMVC中的Model和Session屬性
這篇文章主要介紹了深入理解springMVC中的Model和Session屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring data elasticsearch使用方法詳解
這篇文章主要介紹了Spring data elasticsearch使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Java中日期與時(shí)間的處理及工具類(lèi)封裝詳解
在項(xiàng)目開(kāi)發(fā)中免不了有對(duì)日期時(shí)間的處理,但Java中關(guān)于日期時(shí)間的類(lèi)太多了,本文就來(lái)介紹一下各種類(lèi)的使用及我們項(xiàng)目中應(yīng)該怎么選擇吧2023-07-07
SpringBoot創(chuàng)建定時(shí)任務(wù)的示例詳解
在Spring Boot中創(chuàng)建定時(shí)任務(wù),通常使用@Scheduled注解,這是Spring框架提供的一個(gè)功能,允許你按照固定的頻率(如每天、每小時(shí)、每分鐘等)執(zhí)行某個(gè)方法,本文給大家介紹了SpringBoot創(chuàng)建定時(shí)任務(wù)的示例,需要的朋友可以參考下2024-04-04
SiteMesh如何結(jié)合Freemarker及velocity使用
這篇文章主要介紹了SiteMesh如何結(jié)合Freemarker及velocity使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
淺談Sharding-JDBC強(qiáng)制路由案例實(shí)戰(zhàn)
本文主要介紹了淺談Sharding-JDBC強(qiáng)制路由案例實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
基于Java隨機(jī)生成手機(jī)短信驗(yàn)證碼的實(shí)例代碼
這篇文章主要介紹了Java隨機(jī)生成手機(jī)短信驗(yàn)證碼的實(shí)例代碼,代碼分為哦簡(jiǎn)單版和復(fù)雜版,需要的朋友可以參考下2019-04-04
java使用wait和notify實(shí)現(xiàn)線程通信
這篇文章主要為大家詳細(xì)介紹了java如何使用wait和notify實(shí)現(xiàn)線程之間通信,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10

