Struts 2 ——ActionContext和ServletActionContext区别
来源:互联网 发布:公安大数据情报分析 编辑:程序博客网 时间:2024/06/05 22:39
一、ActionContext
顾名思义,ActionContext是Action执行时的上下文,实质为一个Map容器,用于存放Action在执行时需要用到request\response\session等对象,在action 和jsp间实现数据传递。可通过put,get方法可对ActionContext进行赋值取值操作;
在实际使用中,经常出现Action和JSP页面间数据传递的操作;例如前台传给JSP用户基本信息;Action中将底层查到的数据存入session 或值栈中,前台通过JSTL标签和EL表达式,查找该数据的getset方法,获取Action中属性,从而将数据显示页面;
Action与页面传值:
通过ActionContext容器中的值栈的set方法或是session的put方法,均可将Action中获得的数据存入,前台通过java特定的如EL表达式获取。
<span style="font-size:24px;">ActionContext.getContext().getValueStack().set("user",user);ActionContext.getContext().getSession().put("user",user);</span>前台:
<span style="font-size:24px;">用户名:<s:property value="user.username ">性别:<s:property value="user.sex"></span>注意:
1、user.username 应对应UserAction中getUsername方法,方才可获取用户名。因为java中面向对象的机制,所以对于某个Action类,均通过实例化对象后,通过get set方法获取或设定action属性值。
2、若Action将值存入session中时需要通过添加“#”获值,形如:value="#user.username“,而存入值栈中时则可直接user.username获取
二、ServletActionContext
ServletActionContext类直接继承了ActionContext,所以除了获取值栈、session对象之外,它还提供了直接与Servlet相关对象(request 和response)访问的功能。
<span style="font-size:24px;">ServletActionContext.getRequest().getSession().setAttribute("user",user);ServletActionContext.getRequest().getSession().getAttribute(user);</span></span>
<span style="font-size:24px;">ServletActionContext.getContext().getValueStack().set("user",user);ServletActionContext.getContext().getValueStack().push(user);</span>注意:
ActionContext能够实现我们的功能,最好就不要使用ServletActionContext,尽量避免Action直接访问Servlet的对象,避免耦合度过高。
- Struts 2 ——ActionContext和ServletActionContext区别
- ActionContext 和 ServletActionContext区别
- ActionContext和ServletActionContext区别
- ActionContext和ServletActionContext区别
- ActionContext和ServletActionContext区别
- ActionContext和ServletActionContext区别
- ActionContext和ServletActionContext区别
- ActionContext和ServletActionContext区别
- Struts ActionContext和ServletActionContext小结
- Struts中ActionContext和ServletActionContext
- ActionContext和ServletActionContext的区别
- ActionContext和ServletActionContext的区别
- actionContext和ServletActionContext的区别:
- actionContext和ServletActionContext的区别:
- Actioncontext和ServletActioncontext的区别
- ActionContext和ServletActionContext的区别
- Struts中ActionContext和ServletActionContext的比较
- Struts中ActionContext和ServletActionContext的比较
- 关于LayoutInflater的一些总结
- Android——使用Volley+fastJson在新线程中读取网络接口获取天气信息
- beanutils.describe 方法漏洞
- 单点登录----->在域名相同的情况下实现单点登录(sso)
- Hibernate 调用原生态Sql语句和存储过程
- Struts 2 ——ActionContext和ServletActionContext区别
- hibernate双边的多对多关系
- android 如何在string里使用%
- 排列组合基本公式
- java Unsafe类的compareAndSwap方法
- POJ题目分类 (膜拜经典)
- 递归法和循环法判断数组是否递增
- 手游创业漫话三 (我要做什么游戏 上)
- 4.Swift 自定义View