struts2参数转换器用法

贴代码

struts的action中接收简单类型参数,struts2可以自动转化,但是复杂类型需要自定义转换器转换

public class ConvertAction extends ActionSupport{

    private int age;
private Date date;
private List<String> interests;
private String name;
private Map<String, String> map; //复杂类型
private List<Point> ps;
public List<Point> getPs() {
return ps;
}
public void setPs(List<Point> ps) {
this.ps = ps;
}
//特殊类型
private Point p; public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
} public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public int getAge() {
return age;
}
public Date getDate() {
return date;
}
public List<String> getInterests() {
return interests;
}
public String getName() {
return name;
}
public String hello(){ System.out.println("date:"+date);
return SUCCESS;
}
public void setAge(int age) {
this.age = age;
}
public void setDate(Date date) {
this.date = date;
}
public void setInterests(List<String> interests) {
this.interests = interests;
} public void setName(String name) {
this.name = name;
}
}
public class PointConvert extends DefaultTypeConverter{

    @Override
public Object convertValue(Object value, Class toType) {
// TODO Auto-generated method stub
if (toType == Point.class) {
Point p=new Point();
String[] values=(String[]) value;
String[] pv = values[0].split(",");
p.x=Integer.parseInt(pv[0]);
p.y=Integer.parseInt(pv[1]);
return p;
}
if (toType == String.class) {
return value.toString();
}
return super.convertValue(value, toType);
} }

注册转换器:两种方式

1.局部:与action同包  XXXAction-conversion.properties

struts2参数转换器用法

2.全局:src根目录  xwork-conversion.properties

struts2参数转换器用法

目录结构

struts2参数转换器用法

具体代码在文件那,下载看。

上一篇:Unity应用架构设计(4)——设计可复用的SubView和SubViewModel(Part 2)


下一篇:洛谷教主花园dp