与Servlet耦合的访问方式

直接访问Servlet API将使Action与环境Servlet环境耦合在一起,测试时需要有Servlet容器,不便对Action的单元测试。

直接获取HttpServletRequest对象:

servletActionContext.getRequest()

获取HttpSession:ServletActionContext.getRequest().getSession()

直接获取ServletContext对象:

servletActionContext.getServletContext()

通过实现ServletRequestAware,ServletContextAware等接口的方式。

看代码:

package logan.struts2.study;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; public class TestServletActionContextAction { public String execute(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext servletContext = ServletActionContext.getServletContext(); System.out.println("execute..."); 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> <action name="TestAware" class="logan.struts2.study.TestAwareAction">
<result>/test-aware.jsp</result>
</action> <action name="TestServletActionContextAction" class="logan.struts2.study.TestServletActionContextAction">
<result>/success.jsp</result>
</action> </package> </struts>

success.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> </body>
</html>

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>
<br><br>
<a href="TestAware.action?name=logan">Test Aware</a>
<br><br>
<a href="TestServletActionContextAction">testServletActionContextAction</a>
<%
if(application.getAttribute("date") == null){
application.setAttribute("date", new Date()); }
%> </body>
</html>

访问网页:http://localhost:8080/Struts2-3/index.jsp

下面看第二种方式:

看代码:

package logan.struts2.study;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware; public class TestServletAwareAction implements ServletRequestAware,
ServletContextAware,ServletResponseAware{ @Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println(response); } @Override
public void setServletContext(ServletContext context) {
// TODO Auto-generated method stub
System.out.println(context); } @Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
System.out.println(request); } public String execute(){
return "success";
} }

struts.xml

<?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> <action name="TestAware" class="logan.struts2.study.TestAwareAction">
<result>/test-aware.jsp</result>
</action> <action name="TestServletActionContextAction" class="logan.struts2.study.TestServletActionContextAction">
<result>/success.jsp</result>
</action> <action name="TestServletAware" class="logan.struts2.study.TestServletAwareAction">
<result>/success.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>
<br><br>
<a href="TestAware.action?name=logan">Test Aware</a>
<br><br>
<a href="TestServletActionContextAction">testServletActionContextAction</a>
<br><br>
<a href="TestServletAware">estServletAware</a>
<%
if(application.getAttribute("date") == null){
application.setAttribute("date", new Date()); }
%> </body>
</html>

运行输出结果:

org.apache.struts2.dispatcher.StrutsRequestWrapper@68614863
org.apache.catalina.connector.ResponseFacade@1f70cda2
org.apache.catalina.core.ApplicationContextFacade@428669b6

通过实现ServletXXXAware接口的方式可以有Struts2注入

需要的Servlet相关的对象

ServletRequestAware:注入HttpServletRequest对象(比较常用)

ServletContextAware:注入ServletContext对象(比较常用)

ServletReponseAware:注入HttpServletResponse对象

Struts2学习第五课 通过和ServletAPI耦合的方式获取WEB资源的更多相关文章

  1. Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍

    原文:Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本 ...

  2. 【原创】bootstrap框架的学习 第五课

    一.Bootstrap 中定义了所有的 HTML 标题(h1 到 h6)的样式. <!DOCTYPE html> <html> <head> <title&g ...

  3. Struts2学习(五)———— s标签和国际化

    一.s标签 在struts-2.3.15.1/docs/WW/docs/tag-reference.html下,就有着struts2所有标签的参考文献,只能看看其中比较常用的标签.其他的以后遇到了在看 ...

  4. Struts2学习第三课 访问Web资源

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

  5. struts2基础——请求与响应、获取web资源

    一.请求与响应 Action1.含义:(1) struts.xml 中的 action 元素,也指 from 表单的 action 属性,总之代表一个 struts2 请求.(2) 用于处理 Stru ...

  6. Struts2学习笔记五 拦截器

    拦截器,在AOP中用于在某个方法或字段被访问之前,进行拦截,然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. Struts2中,拦截器是动态拦截Action调用的对象.它提供了一种机制可以使 ...

  7. Struts2学习第八课 声明式异常处理

    异常处理:exception-mapping元素 exception-mapping元素:配置当前的action的声明式异常处理 exception-mapping元素有两个属性: --excepti ...

  8. Struts2学习第七课 OGNL

    request变成了struts重写的StrutsRequestWrapper 关于值栈: helloWorld时,${productName}读取productName值,实际上该属性并不在requ ...

  9. Struts2学习第七课 result

    result 是action节点的子节点 result 代表action方法执行后,可能去的一个目的地 一个action节点可以配置多个result子节点. result的name属性值对应着acti ...

随机推荐

  1. grep egrep

    grep: Global search REgular expression and Print out the line. 作用: 文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查:打 ...

  2. Nginx Rewrite语法详解

    重写中用到的指令 if  (条件) {}  设定条件,再进行重写 set #设置变量 return #返回状态码  return 403; break #跳出rewrite rewrite #重写 I ...

  3. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  4. 2015 ETSI NFV用例指南

    译者简介:忍忍鱼,曾经从SDNLAB获取了很多知识,现在努力为SDNLAB贡献自己的力量.爱学习,求进步!SDNLAB,么么哒! ETSI NFV ISG已经确定了9个潜在的NFV用例.本章节简单介绍 ...

  5. IE6、IE7、IE8及其他浏览器多个元素并排显示

    IE6.IE7.IE8及其他浏览器多个元素并排显示 HTML代码 <div class="line"> <h1>全部input框</h1> &l ...

  6. HDU 4034 Graph:反向floyd

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...

  7. 分享知识-快乐自己:Liunx 安装 Zookeeper

    Zookeeper可以安装在windows中也可以安装在linux中! 但是我们的服务器基本都是在linux之上 安装我们的 zookeeper 注册中心! 安装成功之后  修改 nat 模式对应的端 ...

  8. POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3 ...

  9. Oracle_Exception_01_The Network Adapter could not establish the connection

    1.IP错误或端口错误. 在设置URL时错误,例如 url="jdbc:oracle:thin:@192.168.1.11:1521:mas" 数据库服务器不正确:ping 服务器 ...

  10. Java微信开发_Exception_03_errcode:48001 errmsg:api unauthorized hint

    创建菜单时出现这个异常, 异常信息:errcode:48001 errmsg:api unauthorized hint 解读:调用的接口没有权限. 回去公众号后台一看,发现真的没有自定义菜单的权限, ...