生活随笔
收集整理的這篇文章主要介紹了
【SpringMVC】SpringMVC 对 Date 类型转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringMVC 對 Date 類型轉換
1. 在 springmvc.xml 中配置,代碼中不需要做任何修改
1.1 必須額外導入joda-time.jar
1.2 時間類型 java.sql.Date
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.bjsxt.controller"></context:component-scan><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><bean id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="registerDefaultFormatters" value="false" /><property name="formatters"><set><beanclass="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" /></set></property><property name="formatterRegistrars"><set><beanclass="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"><property name="dateFormatter"><beanclass="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean"><property name="pattern" value="yyyy-MM-dd" /></bean></property></bean></set></property></bean><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/images/" mapping="/images/**"></mvc:resources><mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
</beans>
2. 使用注解. 在需要轉換的參數或實體類屬性上添加
@DateTimeFormatter(pattern=”表達式”)
2.1 使用 Date 參數接收
@Controller
public class DemoController {@RequestMapping("demo")public String
demo(DemoDate demo
) {System
.out
.println("In DemoController, demo = " + demo
);return "abc.jsp";}
}
2.2 在pojo實體類中
public class DemoDate {@DateTimeFormat(pattern
="yyyy/MM/dd")private Date time
;
2.3 注意地方:
2.3.1 不需要導入額外jar
2.3.2 Date 是 java.util.Date
總結
以上是生活随笔為你收集整理的【SpringMVC】SpringMVC 对 Date 类型转换的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。