SpringMVC綁定日期類型參數(shù)

  • Controller層代碼
@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/handle02")
    public ModelAndView handle02(Date birthday) {
        //服務(wù)器時(shí)間
        Date date = new Date();
        //返回服務(wù)器時(shí)間到前端頁(yè)面
        //封裝了數(shù)據(jù)和頁(yè)面信息的modelAndView
        ModelAndView modelAndView = new ModelAndView();
        //addObject 其實(shí)是向請(qǐng)求域中request.setAttribute("date",date)
        modelAndView.addObject("date", date);
        //視圖信息(封裝跳轉(zhuǎn)的頁(yè)面信息)
        modelAndView.setViewName("success");
        System.out.println("date: " + date);
        System.out.println("birthday" + birthday);
        return modelAndView;
    }
}
  • 頁(yè)面訪問(wèn)HTTP報(bào)400錯(cuò)誤


    HTTP 400
  • 控制臺(tái)輸出警告
[INFO] Initializing Servlet 'springmvc'
[INFO] Completed initialization in 677 ms
[WARNING] Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2021-08-16'; nested exception is java.lang.IllegalArgumentException]

從控制臺(tái)的日志上可以看出,SpringMVC沒(méi)有找到對(duì)應(yīng)的類型轉(zhuǎn)換器。

  • 自定義時(shí)間類型轉(zhuǎn)換器
    實(shí)現(xiàn)Spring core包下的Converter接口,實(shí)現(xiàn)自定義轉(zhuǎn)化器
import org.springframework.core.convert.converter.Converter;

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

/**
 * @Version 1.0
 */
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        // 完成字符串向日期的轉(zhuǎn)換
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date parse = simpleDateFormat.parse(source);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

修改Spring MVC配置文件,加載自定義時(shí)間類型轉(zhuǎn)換器

        <!--
        自動(dòng)注冊(cè)最合適的處理器映射器,處理器適配器(調(diào)用handler方法)
        -->
        <mvc:annotation-driven conversion-service="conversionServiceBean"/>
        <!--注冊(cè)自定義類型轉(zhuǎn)換器-->
        <bean id="conversionServiceBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
                <property name="converters">
                        <set>
                                <bean class="com.erxiao.edu.converter.DateConverter"></bean>
                        </set>
                </property>
        </bean>
  • 驗(yàn)證時(shí)間類型攔截器生效
    頁(yè)面正常能夠訪問(wèn),沒(méi)有錯(cuò)誤信息


    HTTP 請(qǐng)求

    控制臺(tái)日志正常輸出,無(wú)異常信息

INFO] Initializing Servlet 'springmvc'
[INFO] Completed initialization in 648 ms
date: Mon Aug 16 17:49:01 CST 2021
birthdayMon Aug 16 00:00:00 CST 2021
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容