Struts2 学习系列 (2) 访问web资源
2016-09-29 00:00
337 查看
在 Action 中, 可以通过以下方式访问 web 的 HttpSession, HttpServletRequest, HttpServletResponse 等资源
I. 和 Servlet API 解耦的方式: 只能访问有限的 Servlet API 对象, 且只能访问其有限的方法(读取请求参数, 读写域对象的属性, 使 session 失效...).
> 使用 ActionContext
> 实现 XxxAware 接口
> 选用的建议: 若一个 Action 类中有多个 action 方法, 且多个方法都需要使用域对象的 Map 或 parameters, 则建议使用
Aware 接口的方式
> session 对应的 Map 实际上是 SessionMap 类型的! 强转后若调用其 invalidate() 方法, 可以使其 session 失效!
II. 和 Servlet API 耦合的方式: 可以访问更多的 Servlet API 对象, 且可以调用其原生的方法.
> 使用 ServletActionContext直接访问 Servlet API 将使 Action 与 Servlet 环境耦合在一起, 测试时需要有 Servlet 容器, 不便于对 Action 的单元测试.
> 实现 ServletXxxAware 接口.
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
I. 和 Servlet API 解耦的方式: 只能访问有限的 Servlet API 对象, 且只能访问其有限的方法(读取请求参数, 读写域对象的属性, 使 session 失效...).
> 使用 ActionContext
ActionContext actionContext = ActionContext.getContext(); Map<String, Object> sessionMap = actionContext.getSession(); Map<String, Object> applicationMap = actionContext.getApplication(); Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request"); requestMap.put("requestKey", "requestValue"); Map<String, Object> parameters = actionContext.getParameters(); System.out.println(((String[])parameters.get("name"))[0]); parameters.put("age", 100);
> 实现 XxxAware 接口
import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.ParameterAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; public class TestAwareAction implements ApplicationAware, SessionAware, RequestAware, ParameterAware{ private Map<String, Object> application; @Override public void setApplication(Map<String, Object> application) { this.application = application; } @Override public void setParameters(Map<String, String[]> parameters) { } @Override public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub } public String execute(){ //1. 向 application 中加入一个属性: applicationKey2 - applicationValue2 application.put("applicationKey2", "applicationValue2"); //2. 从 application 中读取一个属性 date, 并打印. System.out.println(application.get("date")); return "success"; } }
> 选用的建议: 若一个 Action 类中有多个 action 方法, 且多个方法都需要使用域对象的 Map 或 parameters, 则建议使用
Aware 接口的方式
> session 对应的 Map 实际上是 SessionMap 类型的! 强转后若调用其 invalidate() 方法, 可以使其 session 失效!
II. 和 Servlet API 耦合的方式: 可以访问更多的 Servlet API 对象, 且可以调用其原生的方法.
> 使用 ServletActionContext直接访问 Servlet API 将使 Action 与 Servlet 环境耦合在一起, 测试时需要有 Servlet 容器, 不便于对 Action 的单元测试.
HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); ServletContext application = session.getServletContext();
> 实现 ServletXxxAware 接口.
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
相关文章推荐
- Struts2 学习系列 (2) 访问web资源
- Struts2学习——(四)访问web资源
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- Struts2-访问web 资源
- Struts2-学习笔记系列(4)-访问servlet api
- JavaWeb框架之Struts2 ---- 系列学习
- Struts2学习第三天:访问web元素
- Struts2学习---简单的数据校验、访问Web元素
- Struts2_005_访问 web 资源
- Struts2_学习笔记(五)----访问Web元素、include、默认Action、Action总结
- struts2 在Action中访问WEB资源
- Struts2学习(三)Web资源获取
- Struts2学习----------Web资源获取
- Struts2获取Web容器资源的方式 访问request,session
- ssh学习:struts2的获取web资源模式
- Struts2学习——0700访问Web元素
- Struts2-Action-7-系列问题(Action中的web元素访问)
- struts2学习_第五天(web元素的访问)
- 在 Action 中访问 WEB 资源(struts2获取HttpServletRequest, HttpSession, ServletContext)
- Struts2(四):在Action中如何访问Web资源