用struts2获取session、request、parmeter的方法
package com.hanqi.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class TestAction { //解耦方式 //封装了WEB资源 //使用ActionContext @SuppressWarnings("unchecked") 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("ses1="+mapSes.get("ses1")); //放入新的值 mapSes.put("ses2","放入的新的session"); mapSes.remove("ses1"); //3、获取request资源 Map<String, Object>mapReq= (Map<String, Object>) ac.get("request"); //不能直接获取之前的request属性 System.out.println("req1="+mapReq.get("req1")); //可以放入新的request的属性 mapReq.put("req2", "放入新的request"); //4、直接获取传递的参数 Map<String, Object>mapPar=ac.getParameters(); //参数值被封装成String数组 String[] str=(String[])mapPar.get("user"); for(String t:str) { System.out.println("user="+t); } //放入新的参数不能被获取到 mapPar.put("pw", new String[]{"123456"}); return "success"; } }
package com.hanqi.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 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("ses1="+mapSes.get("ses1")); mapSes.put("ses2", "以Aware放入的Session"); //访问 request System.out.println("req="+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[]{"12345"}); return "success"; } @Override public void setSession(Map<String, Object> arg0) { mapSes=arg0; } @Override public void setRequest(Map<String, Object> arg0) { mapReq=arg0; } @Override public void setParameters(Map<String, String[]> arg0) { mapPar=arg0; } }
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资源 //得到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("ses1="+hs.getAttribute("ses1")); hs.setAttribute("ses2", "原生方式放入的内容"); //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"; } }
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) { hsr=arg0; hs=arg0.getSession(); } public String testServletAware() { System.out.println("app1= "+sc.getAttribute("app1")); sc.setAttribute("app2", "新的app2"); System.out.println("ses1= "+hs.getAttribute("ses1")); hs.setAttribute("ses2", "新的ses2"); System.out.println("user= "+hsr.getParameter("user")); return "success"; } }