Struts2类型转化器
Struts2默认地设置有转化器的使用,如果我们要自定义转化器的话,我们可以让一个类去继承DefaultTypeConverter类然后重写public Object convertValue(Object value, Class toType) 方法toType表示要转化的类型,value表示要转化啊的值
以Action内的Date birthday属性为例:
public class MyAction {
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
转化器
如果我们转化的格式不对,需要抛出TypeConversionException才能跳转到,struts.xml内配置的result指定为input属性中
最后注册这个转化器
局部转化器:是指Action的类名+"conversion.properties"例如:"MyAction-conversion.properties"
properties内需要写上要被转化的对象,如birthday则需要写:birthday=com.abc.converters.MyDateConverter如图
全局转化器:是xwork-conversion.properties,properties
写法:java.util.Date=com.abc.converters.MyDateConverter
如图
类型转换异常提示信息的修改
( 1 ) Action 所在包中添加名称为 ActionClassName.properties 的 属 性 文 件 , 其 中
ActionClassName 为 Action 类的类名。
(2)在该文件中写入内容:invalid.fieldvalue.变量名=异常提示信息
在输入的jsp页面的form前加上<s:fielderror/>可将错误value信息显示出来
部分引用动力节点的Struts教程