SpringMVC 处理Date类型@DateTimeFormat <fmt:formatDate/> @InitBinder
2017-03-23 14:59
471 查看
第一种:
在Date类型的属性上加入@DateTimeFormat注解
加入joda相关的包
在SpringMVC配置文件中加入
首先在相对应的属性上加注解:
具体流程:
加入joda的jar包。因为在@DateTimeFormat注解中使用到了joda包中的相关东西,所以缺少这个包也是会报异常的。这里使用maven依赖。
第二需要在SpringMVC配置XML文件中加入配置:
用一个Controller接收:
结束。
第二种:
使用 解决
- 第一步:
第二步
type可以指定具体类型,如下:
举例:
可以和其他标签结合使用
在value里直接使用
3.@InitBinder
所在的Controller里面,加上使用@InitBinder声明的方法
在DataBinder先进行日期处理
在Date类型的属性上加入@DateTimeFormat注解
加入joda相关的包
在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"/> 输出格式: 2010年5月31日 8.<fmt:formatDate value="${date}" type="date" dateStyle="full"/> 输出格式:2010年5月31日 星期一 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"/> 输出格式: 下午11时59分59秒 13.<fmt:formatDate value="${date}" type="time" timeStyle="full"/> 输出格式: 下午11时59分59秒 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 下午 中央夏令时
相关文章推荐
- JSTL之格式化标签<fmt:formatDate>
- JSTL <fmt:formatDate/>
- <fmt:formatDate>标签
- 从数据库取出的Date类型的数据放入Map<String,Object>之前需要做的处理
- 16.5.5: <fmt:formatDate>标签
- JSTL之数字、日期格式化<fmt:formatNumber/>、<fmt:formatDate/>
- jsp页显示时间标签JSTL标签 <fmt:formatDate/> 实例大全
- 日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo
- JSTL <fmt:formatDate/>
- <fmt:formatNumber>和<fmt:formatDate>
- <fmt:formatDate> 标签
- Jstl <fmt:formatDate> problem
- JSTL标签之<fmt:formatDate/>使用介绍
- input 和<fmt:formatDate>的结合使用
- JSTL之数字、日期格式化<fmt:formatNumber/>、<fmt:formatDate/>
- jsp页显示时间标签JSTL标签 <fmt:formatDate/> 实例大全
- springmvc 框架中的数据的绑定:@InitBinder&@DateTimeFormat()
- jstl之数字、日期格式化---<fmt:formatNumber/>、<fmt:formatDate/>
- Jsp页显示时间标签JSTL标签 <fmt:formatDate/> 实例大全
- jstl <fmt:formatDate>标签