1. 程式人生 > >SpringMVC處理Date成員物件報400 Bad Request解決辦法

SpringMVC處理Date成員物件報400 Bad Request解決辦法

在使用SpringMVC的時候,我們可能需要將一個物件從View傳遞給Controller。而當這個Object只是一些簡單的Stringint或者boolean型別的成員變數時,SpringMVC能自動將View層的JSON包含的String型別轉換為Object成員變數相應的型別。但是當這個ObjectDate型別的成員變數的時候,SpringMVC在將String轉換成Date型別時,就會出錯,報異常。但是我們又需要使用Date型別的時候,其實Spring給我們提供了簡單的操作方式可以完成這個任務的。

     SpringMVC 提供了一個註解 @DateTimeFormat 。可以將 View 傳過來的 String型別轉換為 Date 型別。具體使用方式很簡單,直接在成員變數上加入註解就可以了,同時還可以指定 format 的格式,如下所示:

<span class="keyword" style="font-weight: bold;">public</span> <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;"><span class="keyword" style="color: rgb(51, 51, 51);">class</span> <span class="title">Person</span> {</span>

<span class="indent">  </span><span class="keyword" style="font-weight: bold;">private</span> String name;

<span class="indent">  </span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//直接在date型別上加入註解,同時指定格式樣式</span>

<span class="indent">  </span><span class="annotation">@DateTimeFormat</span>( pattern = <span class="string" style="color: rgb(221, 17, 68);">"yyyy-MM-dd"</span> )

<span class="indent">  </span><span class="keyword" style="font-weight: bold;">private</span> Date birthday;

<span class="indent">  </span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//setterAndGetter</span>

}

       至此,不要以為完事大吉了,你還需要完成以下兩個步驟才可以。

第一需要加入 joda 的 jar 包。因為在 @DateTimeFormat 註解中使用到了 joda 包中的相關東西,所以缺少這個包也是會報異常的。如果使用的直接匯入 jar 包的話,去下載 joda-Jar 匯入即可,如果使用的是 Maven 管理專案的 jar ,那麼在配置檔案檔案中加入依賴:

<span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">dependency</span>></span>

    <span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">groupId</span>></span>joda-time<span class="tag" style="color: rgb(0, 0, 128);"></<span class="title">groupId</span>></span>

    <span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">artifactId</span>></span>joda-time<span class="tag" style="color: rgb(0, 0, 128);"></<span class="title">artifactId</span>></span>

    <span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">version</span>></span>2.3<span class="tag" style="color: rgb(0, 0, 128);"></<span class="title">version</span>></span>

<span class="tag" style="color: rgb(0, 0, 128);"></<span class="title">dependency</span>></span>

第二需要在 SpringMVC 配置 xml 檔案中(一般是 dispatchServlet.xml 檔案)中加入配置: <mvc:annotation-driven /> 。這一句配置是一種簡寫,其實是給 Spring 容器中注入了兩個 Bena ,分別是: DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter 。 @DateTimeFormat 註解的內部同樣需要使用到前面注入的兩個 bean 去處理,所以缺少這個配置, Spring 容器中沒有對應的 bean 去處理註解同樣也會報錯。至此,所有的步驟都完成了,可以跑了。

接下來我們跑跑測試一下,測試過程:

首先需要一個表單 :

<span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">form</span> <span class="attribute" style="color: rgb(0, 128, 128);">action</span>=<span class="value" style="color: rgb(221, 17, 68);">"test"</span> <span class="attribute" style="color: rgb(0, 128, 128);">method</span>=<span class="value" style="color: rgb(221, 17, 68);">"post"</span>></span>

    <span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">input</span> <span class="attribute" style="color: rgb(0, 128, 128);">type</span>=<span class="value" style="color: rgb(221, 17, 68);">"text"</span> <span class="attribute" style="color: rgb(0, 128, 128);">name</span>=<span class="value" style="color: rgb(221, 17, 68);">"name"</span>></span>

    <span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">input</span> <span class="attribute" style="color: rgb(0, 128, 128);">type</span>=<span class="value" style="color: rgb(221, 17, 68);">"text"</span> <span class="attribute" style="color: rgb(0, 128, 128);">name</span>=<span class="value" style="color: rgb(221, 17, 68);">"birthday"</span>></span>

    <span class="tag" style="color: rgb(0, 0, 128);"><<span class="title">input</span> <span class="attribute" style="color: rgb(0, 128, 128);">type</span>=<span class="value" style="color: rgb(221, 17, 68);">"submit"</span> <span class="attribute" style="color: rgb(0, 128, 128);">name</span>=<span class="value" style="color: rgb(221, 17, 68);">"提交"</span>></span>

<span class="tag" style="color: rgb(0, 0, 128);"></<span class="title">form</span>></span>

    用一個 Controller 接收:

<span class="annotation">@RequestMapping</span>( <span class="string" style="color: rgb(221, 17, 68);">"/test"</span> )

<span class="keyword" style="font-weight: bold;">public</span> ModelAndView test(HttpServletRequest request,

<span class="indent">  </span>   <span class="annotation">@ModelAttribute</span> Person person) {

<span class="indent">  </span>ModelAndView view = <span class="keyword" style="font-weight: bold;">new</span> ModelAndView();

<span class="indent">  </span>System.out.println(person.toString());

<span class="indent">  </span>view.setViewName(<span class="string" style="color: rgb(221, 17, 68);">"/test/data"</span>);

<span class="indent">  </span><span class="keyword" style="font-weight: bold;">return</span> view;

}

好了,總結一下整個過程,其實就 3 步:

1 、   在 Date 型別的屬性上加入 @DateTimeFormat 註解

2、   加入 joda 相關的包

3、   在 SpringMVC 配置檔案中加入 <mvc:annotation-driven />

注意:第3步在Spring MVC 引入配置檔案時,可能會出現

"mvc:annotation-driven" 的字首 "mvc"未繫結

解決辦法:這是我在spring-servlet.xml檔案裡使用<mvc>開頭的標籤時,忘記引入了名稱空間。在xml的beans裡面加入如下程式碼即可

[html] view plain copy  print?
  1. xmlns:mvc="http://www.springframework.org/schema/mvc"
  2. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
原部落格地址:http://blog.csdn.net/z69183787/article/details/40373565