SpringMVC 處理Date類型@DateTimeFormat @InitBinder

第一種:

  1. 在Date類型的屬性上加入@DateTimeFormat註解
  2. 加入joda相關的包
  3. 在SpringMVC配置文件中加入<mvc:annotation-driver/>
    首先在相對應的屬性上加註解:
public class Person{
    //直接在date類型上加入註解,同時指定格式樣式
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;
}

具體流程:
加入joda的jar包。因爲在@DateTimeFormat註解中使用到了joda包中的相關東西,所以缺少這個包也是會報異常的。這裏使用maven依賴。

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.7</version>
</dependency>

第二需要在SpringMVC配置XML文件中加入配置:<mvc:annotaton-driver/>。這一句配置是一種簡寫,其實是給Spring注入了兩個bean,分別是:DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter註解的內部同樣需要使用到前面注入的兩個bean去處理,所以缺少這個配置,Spring容器中沒有對應的bean去處理註解同樣也會報錯。進入測試:

<form action="test" method="post">
    <input type="text" name="name"/>
    <input type="text" name="birthday"/>
    <input type="submit" name="提交"/>
</form>

用一個Controller接收:

@RequestMapping("/test")
public ModelAndView test(HttpServletRequest request,@ModelAttribute Person person){
    ModelAndView view = new ModelAndView();
    System.out.println(person.toString());
    view.setViewName("/test/data");
    return view;
}

結束。

第二種:
使用 解決
- 第一步:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
  • 第二步
<fmt:formatDate value="轉換對象" type="轉換格式"/>  

type可以指定具體類型,如下:

1.<fmt:formatDate value="${date}" type="both"/>  
輸出格式: 2010-5-31 23:59:59  

舉例:

<td><fmt:formatDate value="${book.bookPublicationDate }" type="date"/></td>

可以和其他標籤結合使用

<input type="text"  name="bookPublicationDate" value="<fmt:formatDate value="${list.bookPublicationDate }" type="date"/>">

在value裏直接使用<fmt:formatDate/> 格式化鎖取到的值


3.@InitBinder
所在的Controller裏面,加上使用@InitBinder聲明的方法
在DataBinder先進行日期處理

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
<fmt:formatDate value="" type=""/>中type的其他類型


2.<fmt:formatDate value="${date}" type="date"/>  
輸出格式: 2010-4-1  

3.<fmt:formatDate value="${date}" type="time"/>  
輸出格式: 23:59:59  

4.<fmt:formatDate value="${date}" type="date" dateStyle="default"/>  
輸出格式:2010-5-31  

5.<fmt:formatDate value="${date}" type="date" dateStyle="short"/>  
輸出格式:04-5-31  

6.<fmt:formatDate value="${date}" type="date" dateStyle="medium"/>  
輸出格式:2010-5-31  

7.<fmt:formatDate value="${date}" type="date" dateStyle="long"/>  
輸出格式: 20105318.<fmt:formatDate value="${date}" type="date" dateStyle="full"/>  
輸出格式:2010531日 星期一  

9.<fmt:formatDate value="${date}" type="time" timeStyle="default"/>  
輸出格式: 23:59:59  

10.<fmt:formatDate value="${date}" type="time" timeStyle="short"/>  
輸出格式:下午11:59  

11.<fmt:formatDate value="${date}" type="time" timeStyle="medium"/>  
輸出格式: 23:59:59  

12.<fmt:formatDate value="${date}" type="time" timeStyle="long"/>  
輸出格式: 下午11595913.<fmt:formatDate value="${date}" type="time" timeStyle="full"/>  
輸出格式: 下午115959秒 CDT  

14.<fmt:formatDate value="${date}" type="both" pattern="EEEE, MMMM d, yyyy 輸出格式: HH:mm:ss Z"/>  
星期四, 四月 1, 2010 13:30:00 -0600  

15.<fmt:formatDate value="${date}" type="both" pattern="d MMM yy, h:m:s a zzzz/>  
輸出格式: 31 五月 04, 11:59:59 下午 中央夏令時  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章