Struts2与ServletAPI解耦
什么是与Servlet API解耦?
为了避免与servlet API耦合在一起,方便Action做单元测试,
Struts2对HttpServletRequest,HttpSession,和ServletContext进行了封装,构造了3个Map对象来替代这三个对象,在Action中可以直接使用HttpServletRequest,HttpSession,ServletContext对应的Map对象来保存和读取数据。
两种解耦方式:
1、 使用Struts2提供的工具类中提供的静态方法,得到对用的封装后对象。
package cn.itcast.context; import java.util.Map; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class ContextAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L; public String test() throws Exception{
System.out.println("ContextAction ****** test()"); HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("username","username_request"); HttpServletResponse response=ServletActionContext.getResponse(); Map sessionMap=ServletActionContext.getContext().getSession();
sessionMap.put("username", "username_session"); ServletContext sc=ServletActionContext.getServletContext();
sc.setAttribute("username", "username_application"); return "attr";
}
}
2、 Action实现ServletRequestAware,ServletResponseAware,ServletContextAware,SessionAware四个接口,分别重写对应的set方法,达到操作该4个封装后对象。
package cn.itcast.context; import java.util.Map; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class Context02Action extends ActionSupport
implements ServletRequestAware,ServletResponseAware,ServletContextAware,SessionAware{ HttpServletRequest request;
HttpServletResponse response;
ServletContext context;
Map<String, Object> sessionMap; private static final long serialVersionUID = 1L; public String test() throws Exception{
System.out.println("ContextAction ****** test()"); HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("username","username_request"); HttpServletResponse response=ServletActionContext.getResponse(); Map sessionMap=ServletActionContext.getContext().getSession();
sessionMap.put("username", "username_session"); ServletContext sc=ServletActionContext.getServletContext();
sc.setAttribute("username", "username_application"); return "attr";
} public void setSession(Map<String, Object> session) {
this.sessionMap=session;
} public void setServletContext(ServletContext context) {
this.context=context; } public void setServletResponse(HttpServletResponse response) {
this.response=response;
} public void setServletRequest(HttpServletRequest request) {
this.request=request; }
}
两种方式没有好坏之分,全凭个人喜好!
其他代码:
<?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> <package name="context" namespace="/context" extends="struts-default">
<action name="contextAction_test" class="cn.itcast.context.ContextAction" method="test">
<result name="success">/context/success.jsp</result>
<result name="attr">/context/attr.jsp</result>
</action>
<action name="contextAction02_test" class="cn.itcast.context.Context02Action" method="test">
<result name="success">/context/success.jsp</result>
<result name="attr">/context/attr.jsp</result>
</action>
</package> </struts>
struts_context.xml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<a href="${pageContext.request.contextPath }/context/contextAction_test.do">textContext</a><br/>
<a href="${pageContext.request.contextPath }/context/contextAction02_test.do">testContext</a><br/>
</body>
</html>
context/test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
xxxxxxxxxxxxxx<br/>
</body>
</html>
context/success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'attr.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
${requestScope.username }<br/>
${sessionScope.username }<br/>
${applicationScope.username }
</body>
</html>
context/attr.jsp
Struts2与ServletAPI解耦的更多相关文章
- Struts2笔记——与ServletAPI解耦
与ServletAPI解耦的访问方式 为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSessi ...
- struts2 之 ServletAPI
1. 在struts2中有两种方式使用SercletAPI,一种解耦方式,一种耦合方式. 2. 解耦方式就是使用ActionContext 来实现,是完全解耦 servletAPI. ActionCo ...
- Struts2访问ServletAPI的三种方式
web应用中需要访问的ServletAPI,通常只有HttpServletRequest,HttpSession,ServletContext三个,这三个接口分别代表jsp内置对象中的request, ...
- struts2访问ServletAPI方式和获取参数的方式
一.访问ServletAPI的三种方式 方式1:通过让Action类去实现感知接口. 此时项目依赖:servlet-api.jar. ServletRequestAware:感知HttpServlet ...
- 2018.11.21 struts2获得servletAPI方式及如何获得参数
访问servletAPI方式 第一种:通过ActionContext (重点及常用 都是获得原生对象) 原理 Action配置 被引入的配置文件 在页面调用取值 第二种:通过ServletAction ...
- java之struts2之ServletAPI
在之前的学习中struts2已经可以处理大部分问题了.但是如果要将用户登录数据存入session中,可以有两种方式开存入ServletAPI. 一种解耦合方式,一种耦合方式. 1. 解耦合方式 解耦合 ...
- 【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】
一.类型转换器 1.在动作类action中,声明和表单中name属性的值同名的属性,提供get和set方法,struts2就可以通过反射机制,从页面中获取对应的内容 package com.kdyzm ...
- 【深入Struts2】获取ServletAPI的三种方式
一:获取servletAPI的三种方法 在传统的Web开发中,经常会用到Servlet API中的HttpServletRequest.HttpSession和ServletContext.Strut ...
- Struts2 第五讲 -- Struts2与Servlet的API解耦
为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSession 和 ServletContext ...
随机推荐
- Robot framework 引入 Selenium2Library 类库:
在用robotframework-selenium2library做web自动化测试时候,首先要将Selenium2Library导入到Test Suite中,在导入Selenium2Library时 ...
- 中兴应用之星携手天翼开放平台:让APP开发更简单
日前,业内率先的APP开发平台运营商中兴应用之星与中国电信天翼开放平台达成战略合作.即广大用户通过天翼开放平台,可直接享受到应用之星提供的"APP开发服务". 应用之星.中兴通 ...
- gulp 静态资源版本控制
package.json { "name": "gulp", "version": "0.0.1", "des ...
- js的常用小技巧
//类对象转成数组 var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*")); ...
- Java8中 Date和LocalDateTime的相互转换
一.在Java 8中将Date转换为LocalDateTime 方法1: 将Date转换为LocalDatetime,我们可以使用以下方法: 1.从日期获取ZonedDateTime并使用其方法toL ...
- go with go
1, vim 安装vim-go 打造GOLANG 专用IDE golang和vim-go安装配置 2, 阅读图书 <Go语言实战> William Kennedy等, 李兆海 译 3,在线 ...
- JavaWeb学习总结第三篇--走进JSP页面元素
JavaWeb学习(三)—走进JSP页面元素 JSP:Java Server Pages,译为Java服务器页面.其脚本采用Java语言,继承了Java所有优点.JSP元素可以分为指令元素.脚本元素和 ...
- ES6之路
从工作到现在,虽然是PHP出身,一直都和JS形影不离,从JQ和原生处理页面,到后来被angular1的MVVM模式惊艳到,再到弃angular转战vue,到现在使用react,一路走来,跳坑无数,现在 ...
- python 基础 2.6 break用法
python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...
- 【BZOJ1969】[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树
[BZOJ1969][Ahoi2005]LANE 航线规划 Description 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由 ...