Struts2接收参数的几种方式

本文详细介绍了Struts2中接收参数的五种常见方式,包括使用Action属性、DomainModel、DTO、ModelDriven和request对象的方法。每种方式都通过示例代码进行了展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts2接收参数的几种方式

其中上述第1-3相对而言较为常用,第4、5方法相对不常用。


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

[java]  view plain copy
  1. public class LoginAction extends ActionSupport {  
  2.     private String username;  
  3.     private String password;  
  4.       
  5.     //对应的get set方法  
  6.     public String getUsername() {  
  7.         return username;  
  8.     }  
  9.     public void setUsername(String username) {  
  10.         this.username = username;  
  11.     }  
  12.     public String getPassword() {  
  13.         return password;  
  14.     }  
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18.     public String execute() {  
  19.         // TODO Auto-generated method stub  
  20.         System.out.println("username = "+username);  
  21.         System.out.println("password = "+password);  
  22.         return SUCCESS;  
  23.     }  
  24. }  

[html]  view plain copy
  1. <form action="login" method="post">  
  2.         用户名:<input type="text" name="username"><br/>  
  3.         密 码:<input type="password" name="password"><br/>  
  4.         <input type="submit" value="提交">  
  5.     </form>  


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

Model类

[java]  view plain copy
  1. public class User {  
  2.     private String username;  
  3.     private String password;  
  4.       
  5.     public String getUsername() {  
  6.         return username;  
  7.     }  
  8.     public void setUsername(String username) {  
  9.         this.username = username;  
  10.     }  
  11.     public String getPassword() {  
  12.         return password;  
  13.     }  
  14.     public void setPassword(String password) {  
  15.         this.password = password;  
  16.     }  
  17. }  

 

Action类

[java]  view plain copy
  1. public class LoginAction extends ActionSupport {  
  2.     private User user;  
  3.     public String execute() {  
  4.         // TODO Auto-generated method stub  
  5.         System.out.println("username = "+user.getUsername());  
  6.         System.out.println("password = "+user.getPassword());  
  7.         return SUCCESS;  
  8.     }  
  9.     public User getUser() {  
  10.         return user;  
  11.     }  
  12.     public void setUser(User user) {  
  13.         this.user = user;  
  14.     }  
  15. }  

 

JSP

[html]  view plain copy
  1. <form action="login" method="post">  
  2.         用户名:<input type="text" name="user.username"><br/>  
  3.         密 码:<input type="password" name="user.password"><br/>  
  4.         <input type="submit" value="提交">  
  5.     </form>  


3. 使用DTO--数据传输对象
它的作用是接收参数,传递参数,并不是项目中的实体类。如用户注册时,会用到确认密码,所以要先把参数接收过
来,做处理后,再传递给相应方法去创建User 对象。提交参数的方式的Domail Model 方式的相同。
DTO:

[java]  view plain copy
  1. public class UserDTO {  
  2.     private String name;  
  3.     private String password;  
  4.     private String confirm;  
  5.   
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     public String getPassword() {  
  13.         return password;  
  14.     }  
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18.     public String getConfirm() {  
  19.         return confirm;  
  20.     }  
  21.     public void setConfirm(String confirm) {  
  22.         this.confirm = confirm;  
  23.     }  
  24. }  

Action:

[java]  view plain copy
  1. public class TestAction extends BaseAction {  
  2.     private static final long serialVersionUID = -7463970150000893325L;  
  3.     private UserDTO userDTO;  
  4.   
  5.     public UserDTO getUserDTO() {  
  6.         return userDTO;  
  7.     }  
  8.     public void setUserDTO(UserDTO userDTO) {  
  9.         this.userDTO = userDTO;  
  10.     }  
  11.     public void execeute() {  
  12.         System.out.println("姓名: " + userDTO.getName());  
  13.     }  
  14. }  

 

4.使用ModelDriven:
在创建Action 的时候,发现Action 实现了ModelDriven 接口,去调用接口的getModel()方法,取到了相关对象。
相应提交方式可以用get 和post

Action类

[java]  view plain copy
  1. public class LoginAction extends ActionSupport implements ModelDriven<User> {  
  2.     private User user = new User();   //需实例化  
  3.     public String execute() {  
  4.         // TODO Auto-generated method stub  
  5.         System.out.println("username = "+user.getUsername());  
  6.         System.out.println("password = "+user.getPassword());  
  7.         return SUCCESS;  
  8.     }  
  9.     public User getUser() {  
  10.         return user;  
  11.     }  
  12.     public void setUser(User user) {  
  13.         this.user = user;  
  14.     }  
  15.     public User getModel() {  
  16.         // TODO Auto-generated method stub  
  17.         return user;  
  18.     }  
  19. }  

 

JSP

[html]  view plain copy
  1. <form action="login" method="post">  
  2.         <!-- 属性可以为 对象.XXX 也可以直接为XXX  则前提Action中要自己实例化Model对象 -->  
  3.         用户名:<input type="text" name="user.username"><br/>  
  4.         密 码:<input type="password" name="password"><br/>  
  5.         <input type="submit" value="提交">  
  6.     </form>  

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

[java]  view plain copy
  1. public class TestAction extends BaseAction {  
  2.     private static final long serialVersionUID = -7463970150000893325L;  
  3.   
  4.     public void execeute() {  
  5.         String name = super.getRequest().getParameter("paraName");  
  6.         System.out.println("姓名:" + name);  
  7.     }  
  8. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值