一.解耦方式
特点:对web资源进行了封装,便于单元测试。
实现:ActionContext和接口方式
1.ActionContext
特点:Action执行的上下文对象。保存了执行Action所需要的所有对象
使用:1.获取ActionContext 2.获取application,session。request,parameter资源 3.打印显示
1.获取ActionContext
ActionContext.getContext()
调用自身的静态方法得到实例
采用的是单例模式
可以再次放入新的键值对,put()
2.session的同application。
3.获取request资源
得到封装request的集合
不能访问得到集合之前的request的属性值
可以使用put()放入新的键值对
4.直接获取传递的参数getParameters
object是String[]类型,为了接收同名的不同参数,使用put放入新值无效
2.接口方式
特点
实现接口方法,由Struts2自动放入web资源
不需要重复获取
注意:该类要继承ApplicationAware ,SessionAware, RequestAware,ParameterAware。
必须有相对应的私有map集合的属性
实现以上四种借口的方法
访问读取
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> 添加测试用的web资源 <% application.setAttribute("app1", "测试的application"); session.setAttribute("set1", "测试的session"); request.setAttribute("req1", "测试的request"); %> <br> <a href="testAction?user=sb&user=tom">测试Action类访问web资源</a> <br><br> <a href="testActionAware?user=sb&user=tom">以Aware方式测试Action类访问web资源</a> </body> </html>
<?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> <!-- 覆盖默认的过滤的扩展名 --> <constant name="struts.action.extension" value="do,action,,"></constant> <!-- 定义包 --> <package name="text" extends="struts-default" > <action name="testAction" class ="com.hanqi.action.TestAction" method="testWeb"> <result type="redirect" name="success">test.jsp</result> </action> <!-- 解耦的接口方式 --> <action name="testActionAware" class="com.hanqi.action.TestActionAware" method="testAware"> <result>test.jsp</result> </action> </package> </struts>
解耦方式
package com.hanqi.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class TestAction { //解耦方式 //封装了web资源 //使用ActionContext public String testWeb() { //访问web资源 System.out.println("经过了Action"); //单利模式 //Action实例的上下文对象 ActionContext ac = ActionContext.getContext(); //1.获取application资源 Map<String, Object> mapApp = ac.getApplication(); System.out.println("app1="+mapApp.get("app1")); //放入新的键值对 mapApp.put("app2", "新放入的application"); //2.获取session资源 Map<String, Object> mapses = ac.getSession(); System.out.println("set1="+mapses.get("set1")); //放入新的session mapses.put("set2", "放入的新的session"); //清除 mapses.remove("set1"); //3.获取request资源 Map<String, Object> mapReq = (Map<String, Object>)ac.get("request"); System.out.println("req1="+mapReq.get("req1"));//不能获取之前的request属性 //可以放入新的 mapReq.put("req2", "放入的新的request"); //4.直接获取传递的参数 Map<String, Object> mapPer = ac.getParameters(); //参数值被封装成String[] String [] str = (String[])mapPer.get("user"); for(String t : str) { System.out.println("user="+t); } //放入新的参数,不能被获取到 mapPer.put("pw",new String[]{"123456"}); return "success"; } }
接口方式
package com.hanqi.action; import java.util.Map; import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.ParameterAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; public class TestActionAware implements ApplicationAware , SessionAware, RequestAware,ParameterAware{ private Map<String, Object> mapApp; private Map<String, Object> mapses; private Map<String, Object> mapReq; private Map<String, String[]> mappar; //实现ApplicationAware接口的方法 //提供给Struts2 进行调用,放入Application的数据集合 @Override public void setApplication(Map<String, Object> arg0) { // 接收 mapApp =arg0; } public String testAware() { //访问application System.out.println("app1="+mapApp.get("app1")); mapApp.put("app2","以Aware方式放入的application"); //访问session System.out.println("set1="+mapses.get("set1")); mapses.put("set2", "以Aware方式放入的session"); //访问request System.out.println("req1="+mapReq.get("req1")); mapReq.put("req2", "以Aware方式放入的request"); //访问parameter String [] str =mappar.get("user"); for(String t : str) { System.out.println("user="+t); } mappar.put("pw", new String[]{"1234"}); return "success"; } @Override public void setSession(Map<String, Object> arg0) { // TODO 自动生成的方法存根 mapses = arg0; } @Override public void setRequest(Map<String, Object> arg0) { mapReq=arg0; } @Override public void setParameters(Map<String, String[]> arg0) { // mappar=arg0; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> 显示application的属性 <br> app1=${app1} <br> app2=${applicationScope.app2}<br><br> 显示session的属性<br> set1=${set1 } <br> set2=${sessionScope.set2} <br><br> 显示request对象<br> req1=${req1 } <br> req2=${req2}<br> <br><br> <% out.println("set2===="+session.getAttribute("set2")); String user = request.getParameter("user"); out.print("<br>user="+user); String pw =request.getParameter("pw"); out.print("<br>pw="+pw); %> </body> </html>
二。耦合方式
特点:
获取原生的web资源
必须在Servlet容器的支持
1.ServletActionContext方式
特点:提供了静态方法,获得原生的资源
getServletContex()
得到原生的ServletContext
相当于内置对象application
getRequest()
得到原生的HttpServletRequest getSession() getParameter(参数名)
不能得到之前的request属性值
package com.hanqi.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; public class TestServletAction { public String testServletAction() { //访问原生的web资源 //1得到Servletcontext,根据与application ServletContext sc = ServletActionContext.getServletContext(); System.out.println("app1="+sc.getAttribute("app1")); sc.setAttribute("app2", "原生方式放入的内容"); //2.访问session HttpServletRequest hsr = ServletActionContext.getRequest(); HttpSession hs = hsr.getSession(); System.out.println("set1="+hs.getAttribute("set1")); hs.setAttribute("set2", "原生方式放入的内容session"); //3.访问request System.out.println("req1="+hsr.getAttribute("req1")); hsr.setAttribute("req2", "原生方式放入的request"); //4.访问parameter System.out.println("user1="+hsr.getParameter("user")); System.out.println("user2="+hsr.getParameter("user")); return "success"; } }
2.接口方式
特点
实现接口方法,由Struts2自动放入web资源
不需要重复获取
API
ServletContextAware void setServletContext(ServletContext arg0)
ServletRequestAware void setServletRequest(HttpServletRequest arg0)
package com.hanqi.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.util.ServletContextAware; public class TestServletActionAware implements ServletRequestAware, ServletContextAware { private ServletContext sc; private HttpServletRequest hsr; private HttpSession hs; @Override public void setServletContext(ServletContext arg0) { sc=arg0; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO 自动生成的方法存根 hsr=arg0; hs=arg0.getSession();//得到session } public String testServletAware() { System.out.println("app1="+sc.getAttribute("app1")); sc.setAttribute("app2", "新的APP2"); System.out.println("set1"+hs.getAttribute("set1")); hs.setAttribute("set2", "新的set2"); System.out.println("user="+hsr.getParameter("user")); hsr.setAttribute("req2", "新的req2"); return "success"; } }