SpringMVC自定義綁定參數(解決參數解析錯誤問題)

SpringMVC自定義綁定參數

背景:因爲在前段發送日期類型的數據的時候,日期的格式有很多,但是SpringMVC在前端控制器(Controller)中的Date類型參數只能夠識別並轉換格式爲"yyyy-mm-dd"這種格式的日期類型,所以在設置日期的時候需要自定義一種日期格式。

步驟:

1.創建轉換器

package com.helong.web.convert;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//轉換接收參數爲日期類型的方法
public class DateConvert implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        if(s != null){
            //設置轉換參數的格式(將一個字符串類型轉換爲一個日期格式)
            //設置之後就不能使用之前的數據格式了,如果不是按照下面的格式進行輸入的話就會報錯
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            try {
                return simpleDateFormat.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

2.在SpringMVC核心配置文件中自定義轉換器(轉換工廠和註解掃描)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!--開啓註解掃描-->
    <context:component-scan base-package="com.helong"/>

    <!--視圖解析器前/後綴設置-->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        &lt;!&ndash;設置前綴&ndash;&gt;
        <property name="prefix" value="/WEB-INF/helong/"/>
        &lt;!&ndash;設置後綴&ndash;&gt;
        <property name="suffix" value=".jsp"/>
    </bean>-->


    <!--配置Converter轉換器  轉換工廠-->
    <bean id="dataConvert" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!--可以配置多個轉換器-->
        <property name="converters">
            <list>
                <bean class="com.helong.web.convert.DateConvert"></bean>
            </list>
        </property>
    </bean>

    <!--註解驅動-->
    <mvc:annotation-driven conversion-service="dataConvert"/>
    
</beans>

 3.在前端控制器中接收參數

package com.helong.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class MyController2 {

    /*接收日期類型的參數的時候2010/01/01這種格式不會發生錯誤
    * 但是由於日期有很多格式,springmvc沒有辦法把其他類型字符串轉換成日期格式。所以需要自己自定義參數綁定。
    * 解決方法:自定義類型轉換器
    * 1.創建轉換器
    * 2.在springmvc核心配置文件中自定義轉換器
    * */
    @RequestMapping("/convert")
    public ModelAndView myform1(Integer age, Date birthday,String name ){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(age);
        //將接收到的日期格式的數據裝換爲自己想要的日期的字符串格式的方法
        System.out.println(new SimpleDateFormat("yyyy-mm-dd").format(birthday));
        System.out.println(name);
        modelAndView.setViewName("/second.jsp");
        return modelAndView;
    }

}

注:SpringMVC的目錄結構

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章