欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在SpringMVC自定義綁定參數(shù)-創(chuàng)新互聯(lián)

這篇文章給大家介紹怎么在SpringMVC自定義綁定參數(shù),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

10余年建站經(jīng)驗, 成都網(wǎng)站制作、成都網(wǎng)站設(shè)計客戶的見證與正確選擇。創(chuàng)新互聯(lián)提供完善的營銷型網(wǎng)頁建站明細報價表。后期開發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。

一、概述

1.3 參數(shù)綁定過程

怎么在SpringMVC自定義綁定參數(shù)

1.2 @RequestParam

如果request請求的參數(shù)名和controller方法的形參數(shù)名稱一致,適配器自動進行參數(shù)綁定。如果不一致可以通過 @RequestParam 指定request請求的參數(shù)名綁定到哪個方法形參上。

對于必須要傳的參數(shù),通過@RequestParam中屬性required設(shè)置為true,如果不傳此參數(shù)則報錯。

對于有些參數(shù)如果不傳入,還需要設(shè)置默認(rèn)值,使用@RequestParam中屬性defaultvalue設(shè)置默認(rèn)值。

可以綁定簡單類型:整型、字符串、單精/雙精度、日期、布爾型。

可以綁定簡單pojo類型

  • 簡單pojo類型只包括簡單類型的屬性。

  • 綁定過程:request請求的參數(shù)名稱和pojo的屬性名一致,就可以綁定成功。

問題:

  • 如果controller方法形參中有多個pojo且pojo中有重復(fù)的屬性,使用簡單pojo綁定無法有針對性的綁定,

  • 比如:方法形參有items和User,pojo同時存在name屬性,從http請求過程的name無法有針對性的綁定到items或user。

二、自定義綁定使用屬性編輯器

springmvc沒有提供默認(rèn)的對日期類型的綁定,需要自定義日期類型的綁定。

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

使用這種方法問題是無法在多個controller共用。

2.2 使用WebBindingInitializer(了解)

使用WebBindingInitializer讓多個controller共用 屬性編輯器。

自定義WebBindingInitializer,注入到處理器適配器中。

如果想多個controller需要共同注冊相同的屬性編輯器,可以實現(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));
  }

}

配置如下:

<!-- 注冊屬性編輯器 -->
  <!-- 注冊屬性編輯器 -->
  <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 實現(xiàn)Converter接口

定義日期類型轉(zhuǎn)換器和字符串去除前后空格轉(zhuǎn)換器。

public class CustomDateConverter implements Converter<String, Date> {

  @Override
  public Date convert(String source) {
    
    try {
      //進行日期轉(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>

關(guān)于怎么在SpringMVC自定義綁定參數(shù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站欄目:怎么在SpringMVC自定義綁定參數(shù)-創(chuàng)新互聯(lián)
地址分享:http://www.chinadenli.net/article16/djcpgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google虛擬主機網(wǎng)站排名用戶體驗網(wǎng)站收錄域名注冊

廣告

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

成都app開發(fā)公司