Struts2学习第五课 通过和ServletAPI耦合的方式获取WEB资源
与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资源的更多相关文章
- Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍
原文:Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本 ...
- 【原创】bootstrap框架的学习 第五课
一.Bootstrap 中定义了所有的 HTML 标题(h1 到 h6)的样式. <!DOCTYPE html> <html> <head> <title&g ...
- Struts2学习(五)———— s标签和国际化
一.s标签 在struts-2.3.15.1/docs/WW/docs/tag-reference.html下,就有着struts2所有标签的参考文献,只能看看其中比较常用的标签.其他的以后遇到了在看 ...
- Struts2学习第三课 访问Web资源
1.什么是WEB资源? HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么访问WEB资源? B/S的应用的Contr ...
- struts2基础——请求与响应、获取web资源
一.请求与响应 Action1.含义:(1) struts.xml 中的 action 元素,也指 from 表单的 action 属性,总之代表一个 struts2 请求.(2) 用于处理 Stru ...
- Struts2学习笔记五 拦截器
拦截器,在AOP中用于在某个方法或字段被访问之前,进行拦截,然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. Struts2中,拦截器是动态拦截Action调用的对象.它提供了一种机制可以使 ...
- Struts2学习第八课 声明式异常处理
异常处理:exception-mapping元素 exception-mapping元素:配置当前的action的声明式异常处理 exception-mapping元素有两个属性: --excepti ...
- Struts2学习第七课 OGNL
request变成了struts重写的StrutsRequestWrapper 关于值栈: helloWorld时,${productName}读取productName值,实际上该属性并不在requ ...
- Struts2学习第七课 result
result 是action节点的子节点 result 代表action方法执行后,可能去的一个目的地 一个action节点可以配置多个result子节点. result的name属性值对应着acti ...
随机推荐
- codeforces 54A
题意:收到礼物的规则为每个假日必收到一份礼物,每K天里至少收到一份礼物,求出N天中收到的礼物的最小数量. 思路:将N天根据假日所在天数分为一段段,当假日与假日之间间隔天数hol[i]>-hol[ ...
- Luogu-3829 [SHOI2012]信用卡凸包
这道题的转化很巧妙,可以把信用卡四个角的圆心看做平面上的点来做凸包,\(ans\)就是凸包周长加上一个圆的周长 // luogu-judger-enable-o2 #include<cmath& ...
- castle windsor学习----- Services and Components 两者的定义
- electron—Chromium有酒,Node有肉
谷歌V8引擎的出现,Node.js的诞生注定要把开发模式“搅乱”. 基于云应用,服务化,定制化的应用需求不断增加后使得传统的winform开发空间越来越小,而原来做前端的空间越来越大,Node.js ...
- php一维数组的创建
php一维数组的创建 (1)通过array函数声明数组 使用array 函数定义数组,该函数返回通过所接受接收的参数建立的数组.array 函数使用的格式如下. $arry_name = array( ...
- spring+mybatis的事务配置
出自:http://kinglixing.blog.51cto.com/3421535/723870 定义一个实体类:Emp.java package com.lixing.scm.entity; p ...
- 深度学习—BN的理解(二)
神经网络各个操作层的顺序: 1.sigmoid,tanh函数:conv -> bn -> sigmoid -> pooling 2.RELU激活函数:conv -> bn -& ...
- python 面试题(一)
1 Python的函数参数传递 看两个例子: Python 1 2 3 4 5 a = 1 def fun(a): a = 2 fun(a) print a # 1 Python ...
- 搭建JavaEE项目是遇到的几个问题
问题描述:无法读取spring mvc的xsd文件 参考http://eric-yan.iteye.com/blog/1908470 schema_reference.4: Failed to rea ...
- PS色调— —通道混合
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); Image=im ...