主要笔记:
主要是struts2通过使用ActionContext的方式,能部分的获取WEB资源。
2. 在 Action 中访问 WEB 资源:
1). 什么是 WEB 资源 ?
HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API。
2). 为什么访问 WEB 资源?
B\S 的应用的 Controller 中必然需要访问 WEB 资源: 向域对象中读写属性, 读写 Cookie, 获取 realPath ....
3). 如何访问 ?
I. 和 Servlet API 解耦的方式: 只能访问有限的 Servlet API 对象, 且只能访问其有限的方法(读取请求参数, 读写域对象的属性, 使 session 失效...).
> 使用 ActionContext
> 实现 XxxAware 接口
> 选用的建议: 若一个 Action 类中有多个 action 方法, 且多个方法都需要使用域对象的 Map 或 parameters, 则建议使用
Aware 接口的方式
> session 对应的 Map 实际上是 SessionMap 类型的! 强转后若调用其 invalidate() 方法, 可以使其 session 失效!
II. 和 Servlet API 耦合的方式: 可以访问更多的 Servlet API 对象, 且可以调用其原生的方法.
> 使用 ServletActionContext
> 实现 ServletXxxAware 接口.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
ActionContext类
package com.hgh.struts2.actioncontext;
import java.util.Map;
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:"+date);
//session
/*获取session的方式
* public Map<String, Object> getSession() {
return (Map<String, Object>) get(SESSION);
}
public Object get(String key) {
return context.get(key);
}
到MAP中的方法,所以context也是一个map
V get(Object key);
研究源码得知,actionContext有一个键值对,获取actionContext.get(“request”)就能获取request对应的map
其实方式都是跟session一样的,只是session封装有了那个健
* */
Map<String, Object> sessionMap = actionContext.getSession();
sessionMap.put("sessionKey", "sessionValue");
//request
//actionContext中没有getRequest方法,要怎么获取?
Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request");
requestMap.put("requestKey", "requestValue");
//4. 获取请求参数对应的 Map, 并获取指定的参数值.
//键: 请求参数的名字, 值: 请求参数的值对应的字符串数组
//注意: 1. getParameters 的返回值为在 Map<String, Object>, 而不是 Map<String, String[]>
// 2. parameters 这个 Map 只能读, 不能写入数据, 如果写入, 但不出错, 但也不起作用!
Map<String, Object> parametersMap = actionContext.getParameters();
//System.out.println("name:"+parametersMap.get("name"));//name:[Ljava.lang.String;@15ac023
System.out.println("name:"+((String[])parametersMap.get("name"))[0]);
parametersMap.put("age", 120);
return "success";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- struts.xml
<struts>
<package name="product" extends="struts-default" namespace="/">
<action name="application" class="com.hgh.struts2.actioncontext.TestActionContext">
<result>/WEB-INF/views/application.jsp</result>
</action>
</package>
</struts>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- index.jsp
<body>
<a href="application.action?name=hgh11">to application</a>
<%
if(application.getAttribute("date") == null)
application.setAttribute("date", new Date());
%>
</body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- application.jsp
<body>
<h3>application jsp</h3>
用applicationScope.applicationKey取值,可行:${applicationScope.applicationKey};
</br>
用EL表达式直接取值,可行:${applicationKey}
</br>
session:${sessionKey}
</br>
request:${requestKey }
</br>
parameters:${age}
</br>
parameters name,parameters里面的参数用el表达式获取不到:${parameters.name}
</body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- ').addClass('pre-numbering').hide(); (this).addClass(′has−numbering′).parent().append( numbering); for (i = 1; i