//解耦,封装了web资源——1.使用ActionContext public String testweb() { //访问web资源 System.out.println("OK"); //单例模式,action实例的上下文对象 ActionContext ac=ActionContext.getContext(); //1获取application资源 Mapmp=ac.getApplication(); System.out.println("app1="+mp.get("app1")); //放入新的键值对 mp.put("app2", "2app"); //2获取Session资源 Map ms= ac.getSession(); System.out.println("ses1="+ms.get("ses1")); ms.put("ses2", "2ses"); //3获取request资源 Map mq=(Map )ac.get("request"); System.out.println("req1"+mq.get("req1")); //取不到之前的值,只可以赋新值 mp.put("req3", "3req"); mq.put("req2", "2req"); //4直接获取传递的参数Parameters Map mu=ac.getParameters(); //返回结果为数组类型 System.out.println("user="+mu.get("user")); //强制转型 String[]str=(String[])mu.get("user"); //遍历 for(String a:str) { System.out.println(a); } //放入新参数不能被获取到 mu.put("pw", new String[]{"1233"});//这么写是错误的 return "success"; }
package com.action;import java.util.Map;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 TestAware implements ApplicationAware,SessionAware,RequestAware,ParameterAware { private Mapmp; private Map ms; private Map mq; private Map mm; //实现ApplicationAware接口的方法 //提供给Struts2,放入application的数据集合,进行调用 @Override public void setApplication(Map arg0) { // TODO 自动生成的方法存根 //接收 mp=arg0; } @Override public void setSession(Map arg0) { // TODO 自动生成的方法存根 ms=arg0; } @Override public void setRequest(Map arg0) { // TODO 自动生成的方法存根 mq=arg0; } @Override public void setParameters(Map arg0) { // TODO 自动生成的方法存根 mm=arg0; } public String testAware() { //1访问application System.out.println("app1="+mp.get("app1")); mp.put("app2", "2222app"); //2访问session System.out.println("ses1="+ms.get("ses1")); ms.put("ses2", "2222ses2"); //3访问request System.out.println("req1="+mq.get("req1"));//之前设置的在这访问不到 mq.put("req2", "2222req2"); //4访问Parameter String[] str=mm.get("user"); for(String a:str) System.out.println("suer="+a); mm.put("pw", new String[]{"122"});//在这设置获取不到 return "success"; } }
package com.action;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;public class TestServlet { public String testServlet() { //访问原生的web资源 //1.得到ServletContext,相当于application ServletContext sc= ServletActionContext.getServletContext(); System.out.println("app1="+sc.getAttribute("app1")); sc.setAttribute("app2", "你妹啊app2"); //2.访问Session HttpServletRequest hsr=ServletActionContext.getRequest();//先得到request HttpSession hs=hsr.getSession();//得到Session System.out.println("ses1"+hs.getAttribute("ses1")); hsr.setAttribute("ses2", "原生2ses2"); //3.访问request System.out.println("req1"+hsr.getAttribute("req1")); hsr.setAttribute("req2", "原生req2"); //4.访问parameter System.out.println(hsr.getParameter("user")); System.out.println(hsr.getParameter("user")); return"success"; } }
package com.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 TestServletAware implements ServletRequestAware, ServletContextAware { private ServletContext sc; private HttpServletRequest hsr; private HttpSession hs; @Override public void setServletContext(ServletContext arg0) { // TODO 自动生成的方法存根 sc=arg0; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO 自动生成的方法存根 hsr=arg0; hs=arg0.getSession(); } public String testsa() { System.out.println("app1="+sc.getAttribute("app1")); sc.setAttribute("app2", "原生的app2"); hsr.setAttribute("req2", "132req2"); System.out.println("ses1="+hs.getAttribute("ses1")); hs.setAttribute("ses2", "123ses2"); System.out.println("user="+hsr.getParameter("user")); return"success"; } }