在Struts2中,Action并没有直接和Servlet API耦合。虽然Struts2 中的Action访问Servlet API麻烦一些,但是这却是Struts2中Action的重要改良之一,方便Action进行单元测试。
尽管Action和Servlet API解耦会带来很多好处,然而在Action中完全不访问Servlet API几乎是不可能的,在实现业务逻辑时,经常要访问Servlet中的对象,如request、session和application等 。
在Struts2中,访问Servlet API有3种方法,分别是实现特定接口,通过ServletActionContext类,通过ActionContext类。
思维导图
实现特定接口访问
package com.pc.web.action; 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; import com.opensymphony.xwork2.ActionSupport; public class ActionTest7 extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware { private HttpServletRequest request; private HttpServletResponse response; private ServletContext application; public String demo(){ System.out.println(request); System.out.println(response); System.out.println(application); System.out.println(request.getSession()); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } @Override public void setServletResponse(HttpServletResponse response) { this.response=response; } @Override public void setServletContext(ServletContext application) { this.application=application; } }
通过ServletActionContext类
package com.pc.struts2.web.action; 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 com.opensymphony.xwork2.ActionSupport; public class ActionAPITest extends ActionSupport { public String useServletAPI() { HttpServletRequest request=ServletActionContext.getRequest(); HttpServletResponse response=ServletActionContext.getResponse(); HttpSession session=request.getSession(); ServletContext application=ServletActionContext.getServletContext(); System.out.println(request); System.out.println(response); System.out.println(session); System.out.println(application); return "success"; } }
通过ActionContext类
package com.pc.web.action; 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.StrutsStatics; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class ActionTest8 extends ActionSupport implements StrutsStatics { private HttpServletRequest request; private HttpServletResponse response; private ServletContext application; public String demo(){ ActionContext context=ActionContext.getContext(); request=(HttpServletRequest) context.get(HTTP_REQUEST); response=(HttpServletResponse) context.get(HTTP_RESPONSE); application=(ServletContext) context.get(SERVLET_CONTEXT); System.out.println(request); System.out.println(response); System.out.println(application); System.out.println(request.getSession()); return SUCCESS; } public String demo2(){ //1.获取session对象 HttpSession session=ServletActionContext.getRequest().getSession(); //存入数据 session.setAttribute("sessionAttr", "hello session attr"); session.getId(); //2.获取ServletContext ServletContext application=ServletActionContext.getServletContext(); application.getRealPath("/WEB-INF/uploads"); //存入数据 application.setAttribute("applicationAttr", "hello application attr"); return SUCCESS; } public String demo3(){ //1.获取ActionContext对象 ActionContext context=ActionContext.getContext(); //2.获取sessionMap Map<String,Object> sessionMap=context.getSession(); sessionMap.put("sessionAttr", "hello session map"); //3.获取applicationMap Map<String,Object> applicationMap=context.getApplication(); applicationMap.put("applicationAttr", "hello application map"); return SUCCESS; } }