strus2中获取表单数据 两种方式 属性驱动 和模型驱动

属性驱动

/**
* 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值
* 如果一个属性在对象栈,在页面上可以根据name属性进行回显
*/

/**
* 属性驱动实现的条件:
* 1、当前请求的action在栈顶,所以action中的属性就暴漏出来了
* 2、获取页面上表单的元素,整合成一个map
* 3、调用setValue方法赋值
*/

 package cn.itcast.struts2.sh;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
import com.opensymphony.xwork2.util.ValueStack; public class UserAction { private String ss;
public String getSs() {
return ss;
}
public void setSs(String ss) {
this.ss = ss;
}
public String setValue(){
ValueStack valueStack = ActionContext.getContext().getValueStack();
/**
* 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值
* 如果一个属性在对象栈,在页面上可以根据name属性进行回显
*/ /**
* 属性驱动实现的条件:
* 1、当前请求的action在栈顶,所以action中的属性就暴漏出来了
* 2、获取页面上表单的元素,整合成一个map
* 3、调用setValue方法赋值
*/
//或者直接给ss赋值,因为当前action会放到StackValue栈顶所以可以取相关的里面的数据
valueStack.setValue("ss", "ss");
List<User> userList = new ArrayList<User>();
List<List<User>> users = new ArrayList<List<User>>();
User user = new User();
user.setUid(1L);
user.setUname("aaa");
userList.add(user);
users.add(userList);
ActionContext.getContext().put("users", users); Map<String, List<User>> map = new HashMap<String, List<User>>();
map.put("userList", userList);
ActionContext.getContext().put("map", map);
return "index";
}
}

当 action中的与表单交互的基本数据项非常多的时候,在一个action中写很多基本元素显得代码非常臃肿,所以建立了一个Javaben 专门用于放基本数据,然后通过模型驱动的形式和页面进行交互,放在StackValue堆栈中

内部原理是用到一个模型驱动的拦截器ModelDrivenInterceptor类中的intercept方法 然后装载数据到自己写的javaben

然后会把 UserAction和 User都放到栈顶StackValue 中的

代码:自己定义的javaben

 package cn.itcast.struts2.sh;

 public class User {
private Long uid;
private String uname;
public Long getUid() {
return uid;
}
public void setUid(Long uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}

action中装载这个javabean

 package cn.itcast.struts2.sh;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
import com.opensymphony.xwork2.util.ValueStack; 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 } private String ss;
public String getSs() {
return ss;
}
public void setSs(String ss) {
this.ss = ss;
}
public String setValue(){
ValueStack valueStack = ActionContext.getContext().getValueStack();
/**
* 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值
* 如果一个属性在对象栈,在页面上可以根据name属性进行回显
*/ /**
* 属性驱动实现的条件:
* 1、当前请求的action在栈顶,所以action中的属性就暴漏出来了
* 2、获取页面上表单的元素,整合成一个map
* 3、调用setValue方法赋值
*/
valueStack.setValue("ss", "ss");
List<User> userList = new ArrayList<User>();
List<List<User>> users = new ArrayList<List<User>>();
User user = new User();
user.setUid(1L);
user.setUname("aaa");
userList.add(user);
users.add(userList);
ActionContext.getContext().put("users", users); Map<String, List<User>> map = new HashMap<String, List<User>>();
map.put("userList", userList);
ActionContext.getContext().put("map", map);
return "index";
}
}

最新文章

  1. 自己动手写一个简单的MVC框架(第二版)
  2. Linux笔记:使用Vim编辑器
  3. MFC 构建、消亡 顺序 (二)--多文档 (MDI)
  4. NGUI 3.5教程(六)Font字体
  5. Erlang - Download and Install for Linux
  6. DOM Mutation Observer
  7. linux samba服务配置
  8. poj100纪念
  9. CentOs 6.8配置yum源
  10. mybatis中union可以用if判断连接,但是&lt;select&gt;中第一个select语句不能被if判断,因此可以从dual表中查询null来凑齐。union如果使用order by排序,那么只能放在最后一个查询语句的位置,并且不能带表名。
  11. python框架之Django(11)-中间件
  12. 浅谈 Java JPDA
  13. Luogu4768 NOI2018归程(最短路径+kruskal重构树)
  14. ICTCLAS中的HMM人名识别
  15. Xcode command line tools
  16. ios开发之--打印bool值
  17. 软工作业No.5 甜美女孩第三周yep
  18. vim编程设置
  19. 使用uwsgi配置django
  20. #pragma GCC system_header用法

热门文章

  1. 一站式学习Wireshark(五):TCP窗口与拥塞处理
  2. vim不保存退出
  3. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-005Spring-Data-JPA例子的代码
  4. wordpress可视化编辑器的开启/关闭
  5. 可扩展Web架构与分布式系统
  6. 【原创】【Android New Features】—— 关于ADT 17的BuildConfig.DEBUG
  7. 生产环境的redis高可用集群搭建
  8. Echarts - js-20160611
  9. Redstone 云观象台 服务器部署 - Nginx配置文件
  10. BZOJ 2342 双倍回文(manacher算法)