springmvc自定義參數轉換


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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM