日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【SpringMVC】SpringMVC 对 Date 类型转换

發布時間:2024/2/28 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 类型转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。