打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
struts2 Converter
1、创建Point类(bean)
2、Action继承ActionSupport
3、建立转换器
4、配置转换器

1、
public class Point {
    private int x;
    private int y;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
   
}

2、
public class PointAction extends ActionSupport{
    private Point point;
    public Point getPoint() {
        return point;
    }
    public void setPoint(Point point) {
        this.point = point;
    }
    public String execute(){
        return "success";
    }
}

3、
public class PointConverter extends DefaultTypeConverter{

    @Override
    public Object convertValue(Map context, Object target, Member member,
            String propertyName, Object value, Class toType) {
        if (Point.class==toType) {
            String[] str = (String[])value;
           
            String firstValue = str[0];
           
            String[] resultValue = firstValue.split(",");
            Point point = new Point();
            point.setX(Integer.parseInt(resultValue[0]));
            point.setY(Integer.parseInt(resultValue[0]));
           
            return point;
        }else if (String.class==toType) {
            Point point = (Point) value;
           
            int x = point.getX();
            int y = point.getY();
           
            String result = "x:"+x+"y:"+y;
           
            return result;
        }
       
       
        return null;
    }
   
}
4.
局部转换器在包下
PointAction-conversion.properties
point=com.openwudi.converter.PointConverter
全局转换器在/src下
xwork-conversion.properties
com.openwudi.bean.Point=com.openwudi.converter.PointConverter
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
struts2学习笔记(3)——struts2的局部类型转换
struts2学习笔记(2)struts2中的类型转换_0-1World
struts2的类型转换
SpringMVC源码总结(九)HandlerMethodArgumentResolver介绍
C#中如何将String类型转换成任意基本类型
DWR的学习文档(Hello World,类型转换,Spring,Annotation)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服