java 自定义 转换器_Java笔记之SpringMVC(七):自定义String到Date的类型转换器
0.說在前面
1.新建converter.jsp
轉換器頁面日期:
提交
${converteredDate }
2.新建ConverterController類
packagecom.springmvc.demo.controller;importjava.util.Date;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.servlet.ModelAndView;
@Controllerpublic classConverterController {
@RequestMapping("/initConverter.action")publicString Init(){return "converter";
}
@RequestMapping("/converter.action")publicModelAndView converter(Date myDate){
System.out.println(myDate.getTime());
ModelAndView mav=newModelAndView();
mav.addObject("converteredDate", myDate);
mav.setViewName("converter");returnmav;
}
}
從頁面展示和后臺打印可以看出yyyy/MM/dd格式的日期字符串會被SpringMVC自動進行日期類型轉換.
4.輸入yyyy-MM-dd格式的日期字符串,再進行提交
preHandle在Controller之前被調用......
四月08, 2020 10:55:20上午 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
警告: Resolved [org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Date': no matching editors or conversion strategy found]
afterCompletion在視圖被訪問之后被調用......
從頁面信息和后臺打印的信息可以看出yyyy-MM-dd格式的日期字符串SpringMVC并不能自動轉換成日期類型,這就需要自定義類型轉換器.
5.新建包com.springmvc.demo.converter,在其中新建String2DateConverter類
packagecom.springmvc.demo.converter;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.springframework.core.convert.converter.Converter;public class String2DateConverter implements Converter{
@OverridepublicDate convert(String dateString) {
System.out.println("進入String2DateConverter轉換器了,開始進行轉換......");
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date date=null;try{
date=format.parse(dateString);
}catch(ParseException e) {
e.printStackTrace();
}returndate;
}
}
6.修改springmvc.xml,添加類型轉換器的配置
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
7.重新輸入yyyy-MM-dd格式的日期字符串,再進行提交
從頁面和后臺上的信息可以看出,yyyy-MM-dd格式的日期字符串被成功轉換成了日期類型.
8.說明
(1).在7的基礎上再輸入yyyy/MM/dd格式的日期字符串就又不能轉換了;
(2).一般項目中的日期格式都是統一的,如果是yyyy/MM/dd格式的就不需要自定義類型轉換器,如果是yyyy-MM-dd格式的就可以使用這種方式自定義類型轉換器;
(3).如果真是遇到了多種格式的日期格式,可以定義攔截器,在preHandle方法中對日期字段處理成統一的格式,再參考(2)中說的進行后續操作.
(4).也可以在轉換器類中做些格式判斷,修改String2DateConverter類如下:
packagecom.springmvc.demo.converter;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.springframework.core.convert.converter.Converter;public class String2DateConverter implements Converter{
@OverridepublicDate convert(String dateString) {
System.out.println("進入String2DateConverter的2.0版本了,開始進行轉換......");
SimpleDateFormat format=null;
Date date=null;if(dateString!=null){//匹配yyyy/MM/dd格式的日期字符串
if(dateString.matches("\\d{4}/\\d{2}/\\d{2}")){
format=new SimpleDateFormat("yyyy/MM/dd");//匹配yyyy-MM-dd格式的日期字符串
}else if(dateString.matches("\\d{4}-\\d{2}-\\d{2}")){
format=new SimpleDateFormat("yyyy-MM-dd");
}try{
date=format.parse(dateString);
}catch(ParseException e) {
e.printStackTrace();
}
}returndate;
}
}
再分別進行yyyy/MM/dd和yyyy-MM-dd格式的日期字符串數據提交,就都可以了.當然,其中的正則表達式寫的并不嚴謹,可以自行彌補不足,正確輸入的兩種格式的日期字符串還是可以滿足的.
例如分別輸入2020/04/08和2020-04-08進行提交
總結
以上是生活随笔為你收集整理的java 自定义 转换器_Java笔记之SpringMVC(七):自定义String到Date的类型转换器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用Java做玫瑰花数_c语言如何输出
- 下一篇: java中四种常用的引用类型_java中