天天看點

關于Struts2 json-plugin Date日期格式的處理

1、先看下pojo類

public class User {

	private String userId;
	private String userName;
	private String userPass;
	private String sex;
	private Date birth;
	private Date inTime;
	private Date outTime;
}
           

 2、再看下struts2的action類

private List<User> users;
private int totalCount;
           

 3、如果用struts2 的json-plugin功能的話,前台解析如下:

var record = new Ext.data.Record.create([
				{name : "userId",type : "string"},
				{name : "userName",type : "string"}, 
				{name : "userPass",type : "string"}, 
				{name : "sex",type : "string"}, 
				{name : "birth",type : 'date',dateFormat : 'Y-m-d'},
				{name : "inTime",type : 'date',dateFormat : 'Y-m-d'},
				{name : "outTime",type : 'date',dateFormat : 'Y-m-d'}
				]);      

這樣是得不到date類型的那幾列資料的, 因為對于birth、inTime、outTime這3個的傳回類型的格式不是Ext所需要的“Y-m-d”類型,是以需要對Date類型稍微處理下,

 在User類中Date類型的get方法前面加上注解@JSON(format="yyyy-MM-dd") 即可。

@JSON(format="yyyy-MM-dd") 
	public Date getBirth() {
		return birth;
	}
	
	@JSON(format="yyyy-MM-dd")
	public Date getInTime() {
		return inTime;
	}

	@JSON(format="yyyy-MM-dd")
	public Date getOutTime() {
		return outTime;
	}