spring mvc时间格式化(注解驱动格式化)

今天在测试spring mvc时间类型数据转换,出现一个问题,现在和大家分享一下:

1.模型文件 User.java代码如下:

package cn.itcast.model;

import java.util.Date;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;

/**
 *
 */
public class User {
	private Integer id;
	private String name;
	private Integer age;
	private String email;
	private String addr;
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;	//Date默认转化的时间格式为:1990/12/19
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	@Override
	public String toString() {
		return "User [addr=" + addr + ", age=" + age + ", birthday=" + birthday
				+ ", email=" + email + ", id=" + id + ", name=" + name + "]";
	}
}
2.前台表单页面:add.jsp

<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>添加用户</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<style type="text/css">
		html,body{
			margin:0;
			padding:0;
			font-size: 12px;
			font-family: "仿宋";
		}
		
		input{
			width:180px;
			height:25px;
		}
		
		.error {  
			color: #ff0000;  
			font-style: italic;  
		}
	</style>
  </head>
  
  <body>
    <div style="width: 400px;margin: 100px auto;">
    	<fieldset style="width: 100%">
    		<legend>添加学生</legend>
    		<form action="${pageContext.request.contextPath}/user/save"  method="POST">
	    		<table border="0" cellpadding="5" cellspacing="0" style="margin: 0 auto;">
	    			<tr>
	    				<td>ID:</td>
	    				<td>
	    					<input type="text" name="id"/>
	    				</td>
	    			</tr>
	    			<tr>
	    				<td>姓名:</td>
	    				<td>
	    					<input type="text" name="name"/>
	    				</td>
	    			</tr>
	    			<tr>
	    				<td>邮箱:</td>
	    				<td>
	    					<input type="text" name="email"/>
	    				</td>
	    			</tr>
	    			<tr>
	    				<td>年龄:</td>
	    				<td>
	    					<input type="text" name="age"/>
	    				</td>
	    			</tr>
	    			<tr>
	    				<td>生日:</td>
	    				<td>
	    					<input type="text" name="birthday"/>
	    				</td>
	    			</tr>
	    			<tr>
	    				<td colspan="2" align="center">
	    					<input type="submit" value="添加" style="width:60px;"/>
	    				</td>
	    			</tr>
	    		</table>
    		</form>
    	</fieldset>
    </div>
  </body>
</html>
</pre>3.UserController.java代码如下
<pre name="code" class="java">package cn.itcast.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.itcast.model.User;

@Controller
@RequestMapping("/user")
public class UserController {
	@RequestMapping("/add")
	public String add(){
		return "user/add";
	}
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(@ModelAttribute("user") User user){
		return "user/success";
	}
}

问题:
 
 
<span style="white-space:pre">	</span>在表单提交时后台服务端报错500,原因为String类型的birthday无法装换为Date类型,那就相当于我再model中添加的@DateTimeFormat注解没有起作用。
解决办法:
<span style="white-space:pre">	</span>添加joda-time.jar到项目中(我的理解:spring Formator只是提供接口,并没有具体的实现,而joda-time相当于实现了该接口)。
 
 

你可能感兴趣的:(spring,mvc时间格式化)