歡迎您光臨本站 註冊首頁

Springmvc自定義參數轉換實現代碼解析

←手機掃碼閱讀     techdo @ 2020-07-08 , reply:0

springmvc的參數綁定有以下幾種方法

1)默認的參數綁定 Request Response Session Model(實現ModelMap)

2)簡單類型參數綁定 方法的形參上(Integer id,String,Double,Boolean)

3)pojo類型

4)包裝類型 QueryVo

5)參數綁定之自定義參數轉換

高級參數綁定
 

1)綁定數組
 

直接在方法的參數上綁定 xxx[] xxx
 

將數組注入對象,用該對象來接受數組

2)綁定list
 

使用包裝類,包裝類中有list集合

自定義參數轉換的步驟

1、在springmvc.xml中配置Conveter轉換器

  <bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">    <!-- 配置 多個轉換器-->    <property name="converters">      <list>        <bean class="com.itheima.springmvc.conversion.DateConveter"/>      </list>    </property>  </bean>

 

2、定義轉換類,實現Conveter接口

DateConveter 類:

  public class DateConveter implements Converter<String, Date>{      public Date convert(String source) {      // TODO Auto-generated method stub      try {        if(null != source){//2016:11-05 11_43-50          DateFormat df = new SimpleDateFormat("yyyy:MM-dd HH_mm-ss");          return df.parse(source);        }      } catch (Exception e) {        // TODO: handle exception      }      return null;    }    }

[techdo ] Springmvc自定義參數轉換實現代碼解析已經有237次圍觀

http://coctec.com/docs/java/show-post-241837.html