SpringMVC处理Date成员对象报400 Bad Request解决办法

来源:互联网 发布:ubuntu deb安装包下载 编辑:程序博客网 时间:2024/05/16 09:12
在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller 。而当这个 Object 只是一些简单的 String  int 或者 boolean 类型的成员变量时,SpringMVC 能自动将 View 层的 JSON 包含的 String 类型转换为 Object 成员变量相应的类型。但是当这个 Object  Date 类型的成员变量的时候, 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.   
  3. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
原博客地址:http://blog.csdn.net/z69183787/article/details/40373565
0 0
原创粉丝点击