由于日期數據有很多種格式,所以springmvc沒辦法把字符串轉換成日期類型。所以需要自定義參數綁定。前端控制器接收到請求后,找到注解形式的處理器適配器,對RequestMapping標記的方法進行適配,并對方法中的形參進行參數綁定。在springmvc這可以在處理器適配器上自定義Converter進行參數綁定。如果使用<mvc:annotation-driven/>可以在此標簽上進行擴展。
1.自定義DataConvertor類, 并實現Convertor接口
1
2
3
4
5
6
7
8
9
10
11
12
|
public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); try { return simpleDateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null ; } } |
2.在springmvc.xml配置文件中注冊轉換器
方法一:通過注解驅動的方式加載轉換器
1
2
3
4
5
6
7
8
9
10
|
<!-- 配置mvc注解驅動 --> < mvc:annotation-driven conversion-service = "conversionService" /> <!-- 配置日期轉換器 --> < bean id = "conversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean" > < property name = "converters" > < set > < bean class = "cn.rodge.ssm.converter.DateConverter" ></ bean > </ set > </ property > </ bean > |
方法二:通過自定義webBinder配置(不常用)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<? 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:p = "http://www.springframework.org/schema/p" xmlns:context = "http://www.springframework.org/schema/context" xmlns:dubbo = "http://code.alibabatech.com/schema/dubbo" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 掃描帶Controller注解的類 --> < context:component-scan base-package = "cn.itcast.springmvc.controller" /> <!-- 轉換器配置 --> < bean id = "conversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean" > < property name = "converters" > < set > < bean class = "cn.itcast.springmvc.convert.DateConverter" /> </ set > </ property > </ bean > <!-- 自定義webBinder --> < bean id = "customBinder" class = "org.springframework.web.bind.support.ConfigurableWebBindingInitializer" > < property name = "conversionService" ref = "conversionService" /> </ bean > <!--注解適配器 --> < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > < property name = "webBindingInitializer" ref = "customBinder" ></ property > </ bean > <!-- 注解處理器映射器 --> < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!-- 加載注解驅動 --> <!-- <mvc:annotation-driven/> --> <!-- 視圖解析器 --> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" /> <!-- jsp前綴 --> < property name = "prefix" value = "/WEB-INF/jsp/" /> <!-- jsp后綴 --> < property name = "suffix" value = ".jsp" /> </ bean > </ beans > |
注意:此方法需要獨立配置處理器映射器、適配器,不再使用<mvc:annotation-driven/>
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/rodge-run/p/6545299.html