以下形式中最常用的是前兩種
1. 使用Action的屬性:
在action 里面定義要接收的參數,並提供相應的setter,getter,和提交參數的名稱一致,
並不用做數據類型的轉換
相應提交方式可以用get 和post
如:testAction? name=admin
jsp:
1 <form action="login" method="post" name="form1"> 2 用戶名:<s:textfield name="username"/><br/> 3 密 碼:<s:password name="password"/><br/> 4 <s:submit value="提交"/> 5 </form>
java:
action:
1 public class TestAction extends ActionSupport{ 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 }
2. 使用DomainModel:
在Action 里面不用很多的屬性,而是用Model 層用到的模型,保存它的一個對象。相應提交方式可以用get 和post,如:testAction? resBananRc.name=admin
jsp:
1 <form action="login" method="post" name="form1"> 2 用戶名:<s:textfield name="users.username"/><br/> 3 密 碼:<s:password name="users.password"/><br/> 4 <s:submit value="提交"/> 5 </form>
java
action:
1 public class TestAction extends ActionSupport{ 2 //錯誤的寫法,不能自己實例化,struts會自動實例化 private Users users = new Users(); 3 private Users users; 4 5 public Users getUsers(){ 6 7 return users; 8 9 } 10 11 public void setUsers(Users users){ 12 13 this.users=users; 14 15 }
entity:
1 public class Users{ 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 }
3. 使用DTO--數據傳輸對象(Data Transfer Object)
它的作用是接收參數和傳遞參數,並不是項目中的實體類。
如用戶注冊時,會用到確認密碼,所以要先把參數接收過來,做處理后,再傳遞給相應方法去創建User 對象。
提交參數的方式的Domain Model 方式的相同。
DTO:
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:
1 public class TestAction extends ActionSupport{ 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
如:testAction? name=admin
jsp:
1 <form action="login" method="post" name="form1"> 2 用戶名:<s:textfield name="username"/><br/> 3 密 碼:<s:password name="password"/><br/> 4 <s:submit value="提交"/> 5 </form>
java
action:
1 public class sysAction extends ActionSupport implements ModelDriven<User>{ 2 private User user = new User(); //手動實例化 3 4 public User getModel() { 5 return user; //返回實例 6 } 7 }
5.使用Request對象:
此方法與與傳統的JSP 等傳接參數一樣,即使用request. getParameter(“”)方法
1 public class TestAction extends ActionSupport{ 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 }