zoukankan      html  css  js  c++  java
  • 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

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

    目录结构

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

  • 相关阅读:
    ubuntu 14.04 安装gvim 后报出warning
    ubuntu 如何搭建svn 服务器
    如何为wordpress 添加favicon
    如何为wordpress 的文章添加分页
    ubuntu 如何添加alias
    wordpress 如何防止盗链
    ubuntu 14.04 如何设置静态ip
    钉钉与金蝶ERP对接开发方式
    金蝶组件无法正常工作
    金蝶补丁安装失败
  • 原文地址:https://www.cnblogs.com/Danial7777777/p/9153659.html
Copyright © 2011-2022 走看看