SpringMVC处理Date类型的成员变量方法
来源:互联网 发布:java jdk中文文档 编辑:程序博客网 时间:2024/06/08 14:15
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 />
- SpringMVC处理Date类型的成员变量方法
- SpringMVC处理Date类型的成员变量方法
- SpringMVC处理Date类型的成员变量方法
- SpringMVC处理Date类型的成员变量方法
- SpringMVC处理Date类型的成员变量方法
- SpringMVC处理Date类型的属性的方法
- springmvc 处理Date类型
- Date时间类型的变量比较方法
- web中Date类型的处理方法
- js处理date类型的各种方法
- Extjs中Date类型处理的方法
- springMVC无法接收date类型数据的全局处理方
- springmvc Date类型的转换
- SpringMVC返回字符串类型Date实现方法
- C++中各种类型的成员变量的初始化方法
- C++中各种类型的成员变量的初始化方法
- C++中各种类型的成员变量的初始化方法
- C++中各种类型的成员变量的初始化方法
- js日期时间API
- Intellij idea快捷键备忘
- 虚幻4 构建光照失败 Lighting build failed. Swarm failed to kick off
- mysql的事务的自动提交
- Android中如何实现按两次返回键退出程序
- SpringMVC处理Date类型的成员变量方法
- HDU 1429胜利大逃亡(续) 解题报告
- Words 2016.6.25
- 大数据处理
- 结构+样式+行为是思想
- git常用命令
- UVALive2038-Strategic game(树形dp or 二分图匹配)
- Words 2016.9.4
- 软考视频