struts2日期类型转换,继承StrutsTypeConverter

struts2日期类型转换,继承StrutsTypeConverter_第1张图片


1.date.jsp页面

		
			
			
			
			
		

2.showdate.jsp页面

自定义转换类型:


3.Users实体类

public class Users {
	private String username;
	private String password;
	private Date birthday;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

4.action类

public class UsersAction extends ActionSupport{
	private Users users;

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("姓名:"+users.getUsername());
		System.out.println("生日:"+users.getBirthday());
		return SUCCESS;
	}
}

5.继承StrutsTypeConverter的类

public class DateConverter extends StrutsTypeConverter {
	//支持转换的多种日期格式,可增加时间格式
	private final DateFormat[] df = {
		new SimpleDateFormat("yyyy年MM月dd日"),
		new SimpleDateFormat("yyyy.MM.dd"),
		new SimpleDateFormat("yyyy-MM-dd"),
		new SimpleDateFormat("MM/dd/yyyy"),
		new SimpleDateFormat("yyyy/MM/dd")
	}; 
	
	/**
	 * 将指定日期的字符串类型转化成日期类型
	 * */
	@Override
	public Object convertFromString(Map context, String[] values, Class toType) {
		String dateStr = values[0];
		for(int i=0;i

6.在src目录下新建一个xwork-conversion.propertions文件

java.util.Date=com.jmx.conversion.DateConverter

7.配置struts.xml


	/showdate.jsp
	/date.jsp


最后发布访问项目就行了。




你可能感兴趣的:(struts2,struts2类型转换)