SpringMVC中如何自定義參數綁定-創(chuàng)新互聯

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

創(chuàng)新互聯公司主營鷹潭網站建設的網絡公司,主營網站建設方案,App定制開發(fā),鷹潭h5成都小程序開發(fā)搭建,鷹潭網站營銷推廣歡迎鷹潭等地區(qū)企業(yè)咨詢

一、概述

1.3 參數綁定過程

1.2 @RequestParam

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

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

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

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

可以綁定簡單pojo類型

簡單pojo類型只包括簡單類型的屬性。  綁定過程:request請求的參數名稱和pojo的屬性名一致,就可以綁定成功。

問題:

如果controller方法形參中有多個pojo且pojo中有重復的屬性,使用簡單pojo綁定無法有針對性的綁定,  比如:方法形參有items和User,pojo同時存在name屬性,從http請求過程的name無法有針對性的綁定到items或user。

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

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

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需要共同注冊相同的屬性編輯器,可以實現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>

三、自定義參數綁定使用轉換器

3.1 實現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;  }}

3.2 配置轉換器

<!-- 轉換器 --><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>

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

網站題目:SpringMVC中如何自定義參數綁定-創(chuàng)新互聯
標題URL:http://muchs.cn/article26/cddejg.html

成都網站建設公司_創(chuàng)新互聯,為您提供商城網站、外貿建站、微信小程序、App開發(fā)、App設計自適應網站

廣告

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

網站優(yōu)化排名