strus2中獲取表單數據 兩種方式 屬性驅動 和模型驅動
屬性驅動
/**
* 當前請求的action在棧頂,ss是棧頂的元素,所以可以利用setValue方法賦值
* 如果一個屬性在對象棧,在頁面上可以根據name屬性進行回顯
*/
/**
* 屬性驅動實現的條件:
* 1、當前請求的action在棧頂,所以action中的屬性就暴漏出來了
* 2、獲取頁面上表單的元素,整合成一個map
* 3、調用setValue方法賦值
*/
1 package cn.itcast.struts2.sh; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import com.opensymphony.xwork2.Action; 9 import com.opensymphony.xwork2.ActionContext; 10 import com.opensymphony.xwork2.ModelDriven; 11 import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor; 12 import com.opensymphony.xwork2.interceptor.ParametersInterceptor; 13 import com.opensymphony.xwork2.util.ValueStack; 14 15 public class UserAction { 16 17 private String ss; 18 public String getSs() { 19 return ss; 20 } 21 public void setSs(String ss) { 22 this.ss = ss; 23 } 24 public String setValue(){ 25 ValueStack valueStack = ActionContext.getContext().getValueStack(); 26 /** 27 * 當前請求的action在棧頂,ss是棧頂的元素,所以可以利用setValue方法賦值 28 * 如果一個屬性在對象棧,在頁面上可以根據name屬性進行回顯 29 */ 30 31 /** 32 * 屬性驅動實現的條件: 33 * 1、當前請求的action在棧頂,所以action中的屬性就暴漏出來了 34 * 2、獲取頁面上表單的元素,整合成一個map 35 * 3、調用setValue方法賦值 36 */ 37 //或者直接給ss賦值,因為當前action會放到StackValue棧頂所以可以取相關的里面的數據 38 valueStack.setValue("ss", "ss"); 39 List<User> userList = new ArrayList<User>(); 40 List<List<User>> users = new ArrayList<List<User>>(); 41 User user = new User(); 42 user.setUid(1L); 43 user.setUname("aaa"); 44 userList.add(user); 45 users.add(userList); 46 ActionContext.getContext().put("users", users); 47 48 Map<String, List<User>> map = new HashMap<String, List<User>>(); 49 map.put("userList", userList); 50 ActionContext.getContext().put("map", map); 51 return "index"; 52 } 53 }
當 action中的與表單交互的基本數據項非常多的時候,在一個action中寫很多基本元素顯得代碼非常臃腫,所以建立了一個Javaben 專門用於放基本數據,然后通過模型驅動的形式和頁面進行交互,放在StackValue堆棧中
內部原理是用到一個模型驅動的攔截器ModelDrivenInterceptor類中的intercept方法 然后裝載數據到自己寫的javaben
然后會把 UserAction和 User都放到棧頂StackValue 中的
代碼:自己定義的javaben
1 package cn.itcast.struts2.sh; 2 3 public class User { 4 private Long uid; 5 private String uname; 6 public Long getUid() { 7 return uid; 8 } 9 public void setUid(Long uid) { 10 this.uid = uid; 11 } 12 public String getUname() { 13 return uname; 14 } 15 public void setUname(String uname) { 16 this.uname = uname; 17 } 18 }
action中裝載這個javabean
1 package cn.itcast.struts2.sh; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import com.opensymphony.xwork2.Action; 9 import com.opensymphony.xwork2.ActionContext; 10 import com.opensymphony.xwork2.ModelDriven; 11 import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor; 12 import com.opensymphony.xwork2.interceptor.ParametersInterceptor; 13 import com.opensymphony.xwork2.util.ValueStack; 14 15 public class UserAction implements ModelDriven<User>{ 16 17 private User model = new User(); 18 19 public User getModel() { 20 // TODO Auto-generated method stub 21 return this.model; 22 } 23 24 private String ss; 25 public String getSs() { 26 return ss; 27 } 28 public void setSs(String ss) { 29 this.ss = ss; 30 } 31 public String setValue(){ 32 ValueStack valueStack = ActionContext.getContext().getValueStack(); 33 /** 34 * 當前請求的action在棧頂,ss是棧頂的元素,所以可以利用setValue方法賦值 35 * 如果一個屬性在對象棧,在頁面上可以根據name屬性進行回顯 36 */ 37 38 /** 39 * 屬性驅動實現的條件: 40 * 1、當前請求的action在棧頂,所以action中的屬性就暴漏出來了 41 * 2、獲取頁面上表單的元素,整合成一個map 42 * 3、調用setValue方法賦值 43 */ 44 valueStack.setValue("ss", "ss"); 45 List<User> userList = new ArrayList<User>(); 46 List<List<User>> users = new ArrayList<List<User>>(); 47 User user = new User(); 48 user.setUid(1L); 49 user.setUname("aaa"); 50 userList.add(user); 51 users.add(userList); 52 ActionContext.getContext().put("users", users); 53 54 Map<String, List<User>> map = new HashMap<String, List<User>>(); 55 map.put("userList", userList); 56 ActionContext.getContext().put("map", map); 57 return "index"; 58 } 59 }