Struts2学习第三课 访问Web资源
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资源的更多相关文章
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Struts2学习第三课 Struts2详解
接着上次的课程 这次我们看struts.xml 修改如下:这里是加上命名空间,默认的是不加,我们手动加上时就要在访问时加上命名空间. <?xml version="1.0" ...
- Struts2 - 通过实现 Aware 接口访问 Web 资源
Action 类通过可以实现某些特定的接口, 让 Struts2 框架在运行时向 Action 实例注入 parameters, request, session 和 application 对应的 ...
- Struts2学习第三课 Action
action VS Action类 action:代表一个Struts2的请求 Action类:能够处理struts2请求的类. 属性的名字必须遵守与JavaBean属性名相同的命名规则. 属性的 ...
- Struts2(四):在Action中如何访问Web资源
1.什么WEB资源? HttpServletRequest,HttpServletRespone,HttpApplication,ServletContext,HttpSession等原生Servle ...
- Struts2在Action中访问WEB资源
什么是WEB资源? 这里所说的WEB资源是指:HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API. 为什么需要访问WEB资 ...
- Struts2 之 Action 类访问 WEB 资源
接着上次博客的内容我继续分享我所学到的知识,和自己在学习过程中所遇到问题以及解决方案.当然,如果读者发现任何问题均可以在下方评论告知我,先谢! 在 Action 中访问 WEB 资源 web 资源 所 ...
- 2.struts2访问web资源(在struts2中获取session,request等等)
什么是web资源:web资源就是指request,response,session,servlet的api 为什么需要访问web资源:因为图片上传,需要获取图片的目录,就需要通过action来访问we ...
- 在Action 中访问web资源
1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...
随机推荐
- 从输入url到浏览器呈现网页发生了什么?
大致能分成两个部分:网络通信与页面渲染 一.网络通信 互联网各个网络设备间的通信均基于TCP/IP协议,此协议将整个过程进行分层,由上至下分别为: 应用层.传输层.网络层和数据链路层 1.输入URL ...
- Linux 上通过rpm安装mysql
安装mysql之前要remove掉系统自带的mysql: rpm -qa | grep "MySQL*" 和rpm -qa | grep mysql 要确保卸载干净 rpm ...
- Linux LVM管理
创建和管理LVM 要创建一个LVM系统,一般需要经过以下步骤: 1. 创建分区 fdisk /dev/sdb 使用分区工具(如:fdisk等)创建LVM分区,方法和创建其他一般分区的方式是一样的,区别 ...
- 算法(Algorithms)第4版 练习 1.5.5
对于quick-find,对每个输入数据对,其最少的循环次数为N(sites) 故对于109 sites和106 input pairs,其总的指令次数为:sum = 10^9 * 10^6 * 10 ...
- Fchart
1.FCF_Column3D.swf <?xml version="1.0" encoding="UTF-8"?> <graph bgcolo ...
- 分享知识-快乐自己:Shiro 退出登陆清空缓存实现
shiro是一个被广泛使用的安全层框架,通过xml配置方式与spring无缝对接,用户的登陆/退出/权限控制/Cookie等管理系统基础功能交给shiro来管理. 一般,在JavaWEB管理平台系统时 ...
- 八 Django框架,模板语言
模板语言就是可以将动态数据在html模板渲染的语言 一.接收值渲染 locals()函数,写在请求响应render()函数里,可以将逻辑处理函数里的变量传到html用模板语言渲染 {{...}}接收一 ...
- C#中string.Empty、""和null 之间的区别
1.C#中string.Empty.""和null 之间的区别 (http://blog.csdn.net/henulwj/article/details/7830615)
- python习题-替换敏感词
#3.有一个文件,里面有一些敏感词汇,如下,如果输入这些词,就用**代替,#然后输出,例如输入今天没吃饭,碰到一个傻逼,原来那个sb是小明.输出今天没吃饭,碰到一个**,原来那个**是小明.#需求分析 ...
- Oracle学习笔记_02_基本SQL
1.select语句 (1)语法 SELECT *|{[DISTINCT] column|expression [alias],...} FROM table; (2)示例: 选择全部列 SELECT ...