Struts2 关于StrutsTypeConvert如何转换DomainModel
Struts2 关于StrutsTypeConvert怎么转换DomainModel
最近看了很多大侠的文章,关于Action字段或者是对于类的整体转换的转换的很多
比如
或者是对一个类的整体转换.例如下面这个对Data类型的转换
这上面两类的转换差不多,只需要在UserAction-conversion.properties里面设置
id = com.xxx.youIdConvert
data = com.xxx.youDataConvert
这里假设youIdConvert youDataConvert 是你自定义的转换器...
但是,当你想实现诸如以下的转换的时候就要小心了
当你想对user中的字段id进行转换时
要是你使用如下的配置文件
UserAction-conversion.properties
user.id = com.xxx.youIdConvert
我试验了很多次 要是使用这样的配置 那么 convertFromString()的方法会被调用
也就是说id会被正确的转换成int
但是 convertToString 方法却不会被调用 即使使用OGNL表达式也不行
必须要进行如下的配置
不需要UserAction-conversion.properties文件
配置一个 User-conversion.properties
id=com.xxx.myIdConvert
最近看了很多大侠的文章,关于Action字段或者是对于类的整体转换的转换的很多
比如
public UserAction extends ActionSupport{ private int id; private String name; //getter and setter.... }
或者是对一个类的整体转换.例如下面这个对Data类型的转换
public UserAction extends ActionSupport{ private int id; private String name; private Data data; //getter and setter....
这上面两类的转换差不多,只需要在UserAction-conversion.properties里面设置
id = com.xxx.youIdConvert
data = com.xxx.youDataConvert
这里假设youIdConvert youDataConvert 是你自定义的转换器...
但是,当你想实现诸如以下的转换的时候就要小心了
public class User { private int id; private String name; //... } public class UserAction extends ActionSupport { User user public String execute() { return SUCCESS; } //..getter and setter }
当你想对user中的字段id进行转换时
要是你使用如下的配置文件
UserAction-conversion.properties
user.id = com.xxx.youIdConvert
我试验了很多次 要是使用这样的配置 那么 convertFromString()的方法会被调用
也就是说id会被正确的转换成int
但是 convertToString 方法却不会被调用 即使使用OGNL表达式也不行
必须要进行如下的配置
不需要UserAction-conversion.properties文件
配置一个 User-conversion.properties
id=com.xxx.myIdConvert