免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

SpringMVC中如何自定義參數(shù)綁定

本篇文章為大家展示了SpringMVC中如何自定義參數(shù)綁定,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

臨淄ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書(shū)合作)期待與您的合作!

一、概述

1.3 參數(shù)綁定過(guò)程

1.2 @RequestParam

如果request請(qǐng)求的參數(shù)名和controller方法的形參數(shù)名稱一致,適配器自動(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)單類型:整型、字符串、單精/雙精度、日期、布爾型。

可以綁定簡(jiǎn)單pojo類型

簡(jiǎn)單pojo類型只包括簡(jiǎn)單類型的屬性。  綁定過(guò)程:request請(qǐng)求的參數(shù)名稱和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ì)日期類型的綁定,需要自定義日期類型的綁定。

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));// }

使用這種方法問(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接口

定義日期類型轉(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>

上述內(nèi)容就是SpringMVC中如何自定義參數(shù)綁定,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享名稱:SpringMVC中如何自定義參數(shù)綁定
路徑分享:http://m.newbst.com/article48/gcseep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站自適應(yīng)網(wǎng)站定制網(wǎng)站外貿(mào)網(wǎng)站建設(shè)搜索引擎優(yōu)化靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)