struts2接收参数的几种形式<转>

1. 用Action的属性:
在action 里面定义要接收的参数,并提供相应的setter,getter,和提交参数的名称一致,并不用做数据类型的转换。
相应提交方式可以用get 和post;  Action = Action + ActionForm

 1      public class TestAction extends ActionSupport{  
 2         private static final long serialVersionUID = -7463970150000893325L;  
 3         private String name;  //特别注意这里只是一个属性,而不是一个对象;与下面第二种方式类似但不同,本Action只需要用到这个属性的时候,就采用这种比较原始的方式。
 4         public String getName() {  
 5             return name;  
 6        }  
 7       
 8        public void setName(String name) {  
 9             this.name = name;  
 10         }  
 11       public void execeute() {  
 12             System.out.println(name + " : " + name);  
 13        } 
 14     } 

2. 使用DomainModel的方式:
在Action 里面不用很多的属性,而是用Model 层用到的模型,保存它的一个
对象。相应提交方式可以用get 和post,

public class TestAction extends ActionSupport{  
        private static final long serialVersionUID = -7463970150000893325L;  
        private ResBananRc resBananRc;  //注意这里是一个类对象,一般是我们手工在pojo里的一个类;与第一种方式不同的地方;
      
        public ResBananRc getResBananRc() {  
            return resBananRc;  
        }  
      
        public void setResBananRc(ResBananRc resBananRc) {  
            this.resBananRc = resBananRc;  
        }  
      
        public void execeute() {  
            System.out.println("姓名: " + resBananRc.getName());  
        }  
    }

3. 使用DTO--数据传输对象<常用>       --疑问:第三种方式不是和第二种方式一样的吗? 
它的作用是接收参数,传递参数,并不是项目中的实体类。如用户注册时,会用到确认密码,所以要先把参数接收过
来,做处理后,再传递给相应方法去创建User 对象。提交参数的方式的DomainModel 方式的相同。

DTO:

public class UserDTO {  
    private String name;  
    private String password;  
    private String confirm;  
  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getPassword() {  
        return password;  
    }  
    public void setPassword(String password) {  
        this.password = password;  
    }  
    public String getConfirm() {  
        return confirm;  
    }  
    public void setConfirm(String confirm) {  
        this.confirm = confirm;  
    }  
}

Action:

public class TestAction extends ActionSupport{  
        private static final long serialVersionUID = -7463970150000893325L;  
        private UserDTO userDTO;  
      
        public UserDTO getUserDTO() {  
            return userDTO;  
        }  
        public void setUserDTO(UserDTO userDTO) {  
            this.userDTO = userDTO;  
        }  
        public void execeute() {  
            System.out.println("姓名: " + userDTO.getName());  
        }  
    }

4.使用ModelDriven:
在创建Action 的时候,Action 实现了ModelDriven 接口,去调用接口的getModel()方法,取到了相关对象。
相应提交方式可以用get 和post,关于ModelDriven 接口介绍:http://blog.csdn.net/li_tengfei/article/details/6098145

public class TestAction extends ActionSupport implements ModelDriven<ResBananRc> {  
        private static final long serialVersionUID = -7463970150000893325L;  
        private ResBananRc resBananRc = new ResBananRc();// 这里要手动的New下  
      
        public ResBananRc getModel() {  
            return resBananRc;  
        }  
        public void execeute() {  
            System.out.println("姓名:" + resBananRc.getName());  
        }  
    }

  

5.使用request对象:     <最原始的方式了>
此方法与与传统的JSP 等传接参数一样,即使用request. getParameter(“”)方法

public class TestAction extends ActionSupport{  
    private static final long serialVersionUID = -7463970150000893325L;  
  
    public void execeute() {  
        String name = super.getRequest().getParameter("paraName");  
        System.out.println("姓名:" + name);  
    }  
}
原文地址:https://www.cnblogs.com/sunliqing/p/3464219.html