1.什么是WEB资源?

HttpServletRequest,HttpSession,ServletContext等原生的Servlet API。

2.为什么访问WEB资源?

B/S的应用的Controller中必然需要访问WEB资源,例如,向域对象中读写属性,读写Cookie,获取realPath等等。

3.如何访问?

在Action中,可以通过一下方式访问web的HttpSession,HttpServletRequest,HttpServletResponse等资源

与Servlet API解耦的访问方式:只能访问有限的Servlet API对象,且只能访问其有限的方法(获取请求参数,读写域对象的属性,使session失效)

为了避免与Servlet API耦合在一起,方便Action做单元测试,struts2对HttpServletRequest,HttpSession和ServletCOntext进行封装,构造了3个Map对象来替代这3个对象,在Action中可以直接使用HttpServletRequest,HttpServletSession,ServletContext对应的Map对象来保存和读取数据。

通过com.opensymphony.xwork2.ActionContext

通过Action实现如下接口:

org.apache.struts2.interceptor.ApplicationAware;

org.apache.struts2.interceptor.RequestAware;

org.apache.struts2.interceptor.SessionAware;

与Servlet API耦合的访问方式:(可以访问更多的Servlet API对象,即可以调用其原生的方法)

通过org.apache.struts2.ServletActionContext

通过实现对应的ServletXxxAware接口

ActionContext是Action执行的上下文对象,在ActionContext中保存了Action执行所需要的所有对象,包括parameters,request,session,application等。

获取HttpSession对应的Map等:

public Map getSession()

获取ServletContext对应的Map对象:

public Map getApplication()

获取请求参数对应的Map对象:

public Map getParameters()

获取HttpServletRequest对应的Map对象:

public Object get(Object key):ActionContext类中没有提供类似getRequest()这样的方法来获取HttpServletRequest对应的Map对象,要得到HttpServletRequest对应的Map对象,可以通过为get()方法传递"request"参数实现。

看代码:

package logan.struts2.study;

import java.util.Map;

import org.apache.struts2.dispatcher.Parameter;

import com.opensymphony.xwork2.ActionContext;

public class TestActionContext {

    public String execute(){
//0.获取ActionContext对象
//ActionContext是Action的上下文对象,可以从中获取到当前Action需要的一切信息
ActionContext actionContext = ActionContext.getContext(); //1.获取application对应的Map,并想其中添加一个属性
//通过调用ActionContext 对象的getApplication()方法来获取application对象的Map对象
Map<String, Object> applicationMap = actionContext.getApplication();
//设置属性
applicationMap.put("applicationKey", "applicationValue");
//获取属性
Object date = applicationMap.get("date");
System.out.println(date); //2.session
Map<String,Object> sessionMap = actionContext.getSession();
sessionMap.put("sessionKey", "sessionValue"); //3.request
//ActionContext中并没有提供getRequest方法来获取Request对应的Map
//需要手工调用get()方法,传入request字符串来获取。
Map<String,Object> requestMap = (Map<String, Object>) actionContext.get("request");
requestMap.put("requestKey", "requestValue"); //4.获取请求参数对应的Map,并获取指定的参数值
//parameters这个Map只能读,不能写。如果写入,不会报错,但是也不起作用。
Map<String,Parameter> parameters = actionContext.getParameters();
System.out.println(parameters.get("name")); return "success";
} }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- action VS Action类
action:代表一个Struts2的一个请求
Action类:能够处理Struts2请求的类
-->
<package name="default" namespace="/" extends="struts-default"> <action name="TestActionContext" class="logan.struts2.study.TestActionContext">
<result>/test-actionContext.jsp</result>
</action> </package> </struts>

index.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="TestActionContext.action?name=logan&name=logan2">Test ActionContext</a> <%
if(application.getAttribute("date") == null){
application.setAttribute("date", new Date()); }
%> </body>
</html>

test-actionContext.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>Test ActionContext Page</h4>
application:${applicationScope.applicationKey }
<br><br>
session:${sessionScope.sessionKey }
<br><br>
request:${requestScope.requestKey }
<br><br> <br><br>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Struts2-2</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Struts2学习第三课 访问Web资源的更多相关文章

  1. [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Struts2学习第三课 Struts2详解

    接着上次的课程 这次我们看struts.xml 修改如下:这里是加上命名空间,默认的是不加,我们手动加上时就要在访问时加上命名空间. <?xml version="1.0" ...

  3. Struts2 - 通过实现 Aware 接口访问 Web 资源

    Action 类通过可以实现某些特定的接口, 让 Struts2 框架在运行时向 Action 实例注入 parameters, request, session 和 application 对应的 ...

  4. Struts2学习第三课 Action

    action  VS  Action类 action:代表一个Struts2的请求 Action类:能够处理struts2请求的类. 属性的名字必须遵守与JavaBean属性名相同的命名规则. 属性的 ...

  5. Struts2(四):在Action中如何访问Web资源

    1.什么WEB资源? HttpServletRequest,HttpServletRespone,HttpApplication,ServletContext,HttpSession等原生Servle ...

  6. Struts2在Action中访问WEB资源

    什么是WEB资源? 这里所说的WEB资源是指:HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API. 为什么需要访问WEB资 ...

  7. Struts2 之 Action 类访问 WEB 资源

    接着上次博客的内容我继续分享我所学到的知识,和自己在学习过程中所遇到问题以及解决方案.当然,如果读者发现任何问题均可以在下方评论告知我,先谢! 在 Action 中访问 WEB 资源 web 资源 所 ...

  8. 2.struts2访问web资源(在struts2中获取session,request等等)

    什么是web资源:web资源就是指request,response,session,servlet的api 为什么需要访问web资源:因为图片上传,需要获取图片的目录,就需要通过action来访问we ...

  9. 在Action 中访问web资源

    1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...

随机推荐

  1. P4619 [SDOI2018]旧试题

    题目 P4619 [SDOI2018]旧试题 Ps:山东的题目可真(du)好(liu),思维+码量的神仙题 推式 求\(\sum_{i=1}^A\sum_{j=1}^B\sum_{k=1}^Cd(ij ...

  2. mysql 在windows server下发生系统错误 1067, 进程意外终止的解决方法

    mysql 在windows server下发生系统错误 1067, 进程意外终止,请检查系统盘下的windows目录下是否存在mysql的配置文件my.ini,如存在,将其删除或改名即可.

  3. HTML特效文字代码大全

     HTML特效文字代码大全一.从右向左移代码<marquee direction=left>需要移动的文字</marquee>二.从左向右移代码<marquee dire ...

  4. castle windsor学习----ComponentModel construction contributors

    public class RequireLoggerProperties : IContributeComponentModelConstruction { public void ProcessMo ...

  5. HDU 3954 Level up(多颗线段树+lazy操作)

    又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...

  6. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  7. bzoj 4773: 负环 floyd

    题目: 对于边带权的有向图,找出一个点数最小的环,使得环上的边权和为负. 2 <= n <= 300. 题解: 我们可以考虑从小到大枚举答案. 然后每次枚举更大的答案的时候就从当前的较小的 ...

  8. stackoverflow打开慢

    C:\Windows\System32\drivers\etc 下的hosts文件最下面添加 127.0.0.1  ajax.googleapis.com

  9. 麻省理工《C内存管理和C++面向对象编程》笔记---第一讲:认识C和内存管理

    最近一年都在用.net和Java,现在需要用C了.昨天看到博客园首页的麻省理工开放课程,就找来看看,正好复习一下.这门<C内存管理和C++面向对象编程>不是那种上来就变量,循环的千篇一律的 ...

  10. 11g 如何添加,替换,移除,迁移 OCR ?

    一: 增加 裸设备上,创建至少280MB的裸设备,权限是640,属主是root:oinstall共享文件系统 Or NFS,创建空文件,权限是640,属主是root:oinstall root用户执行 ...