请教有关struts2 类型转换typeconverter的问题,急

twh1224 2010-02-09
要将"2010-01-08"这样的字符串转换成int类型20100108,页面上显示的时候将int 类型20100108转换成字符串"2010-01-08"

public class userAction extends ActionSupport {
    private User user;
    
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

public class user {
   private String name;
   private int birthday;
}

public class IntConverter extends DefaultTypeConverter {

	@Override
	public Object convertValue(Map context, Object value, Class toType) {
		if(int.class == toType) {
			String[] str = (String[])value;
			String[] params = str[0].split("-");
			StringBuilder sdate = new StringBuilder();
			sdate.append(params[0]);
			sdate.append(params[1]);
			sdate.append(params[2]);
			int date = Integer.parseInt(sdate.toString());
			return date;
		}else if(String.class == toType) {
			String date = (String)value;
			StringBuilder sdate = new StringBuilder();
			sdate.append(date.substring(0, 4));
			sdate.append("-");
			sdate.append(date.substring(4, 6));
			sdate.append("-");
			sdate.append(date.substring(6, date.length()));
			return sdate.toString();
		}
		return null;
	}

}


要将页面上得到的string类型birthday转换成int类型,输出的时候将int类型在转换成string类型

我重写了defaultTypeConverter类的方法,在userAction-conversion.properties中
是这样写的:
user.birthday=com.util.IntConverter



结果有问题,从页面上得到string类型转换成int类型,是可行的,但是将int类型转换成String类型的时候,不可行。

longshao_feng 2010-02-12
运行的结果是什么?
Global site tag (gtag.js) - Google Analytics