博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2与ServletAPI解耦
阅读量:6079 次
发布时间:2019-06-20

本文共 5009 字,大约阅读时间需要 16 分钟。

什么是与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
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
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; }}

 

 

两种方式没有好坏之分,全凭个人喜好!

 

其他代码:

/context/success.jsp
/context/attr.jsp
/context/success.jsp
/context/attr.jsp
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+"/";%>              My JSP 'test.jsp' starting page        
textContext
testContext
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+"/";%>              My JSP 'success.jsp' starting page        
xxxxxxxxxxxxxx
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+"/";%>              My JSP 'attr.jsp' starting page        
${requestScope.username }
${sessionScope.username }
${applicationScope.username }
context/attr.jsp

 

  

转载地址:http://rkhgx.baihongyu.com/

你可能感兴趣的文章
挖一挖C#中那些我们不常用的东西之系列(4)——GetHashCode,ExpandoObject
查看>>
Android Activity初探
查看>>
VC++ CopyFile函数使用方法
查看>>
Android 环境搭建 版本问题
查看>>
android WebView总结
查看>>
JS获取客户端IP地址、MAC和主机名七种方法
查看>>
Python:使用基于事件驱动的SAX解析XML
查看>>
python解析json
查看>>
普林斯顿公开课 算法1-11:并查集的应用
查看>>
MySQL数据库优化详解(收藏)
查看>>
poj 1182 食物链 (带关系的并查集)
查看>>
WINDOWS系统Eclipse+NDK+Android + OpenCv
查看>>
Android获取设备採用的时间制式(12小时制式或24小时制式)
查看>>
linux环境下matlab连接mysql
查看>>
教你实现语音识别(基于科大讯飞)
查看>>
实现多项式的JAVA类
查看>>
c++序列化方法
查看>>
Java中怎样由枚举常量的ordinal值获得枚举常量对象
查看>>
Cocos2d-x 3.1.1 学习日志9--一“上一下其乐无穷”游戏开发系列一
查看>>
如何设置缺省路由 route hosts(仅助记) 马连洼 马连洼 马连洼
查看>>