Struts2 访问web元素
访问web元素的四种方法(耦合,依赖注入)、(耦合,非依赖注入)、(非耦合,依赖注入)、(非耦合,非依赖注入)
耦合:可以得到HttpServletResponse,HttpServletRequest,HttpSession等类型对象。能直接操作到真正的web元素。
非耦合:得到的是由struts封装好的Map类型,只能对范围进行存取值操作。
依赖注入:对象由struts2从外部注入。Action类不需要关心对象是如何得到的。
非依赖注入:对象由Action类自己提供初始化。
耦合
非依赖注入
1 public class UserAction extends ActionSupport{ 2 //类型为真实的web元素:HttpServletResponse,HttpServletRequest等.. 3 private HttpServletResponse resp; 4 private HttpServletRequest req; 5 private HttpSession session; 6 private ServletContext app; 7 8 public String execute() throws IOException{ 9 req = ServletActionContext.getRequest(); //得到request 10 resp = ServletActionContext.getResponse(); //得到response 11 app = ServletActionContext.getServletContext(); //得到application 12 session = ServletActionContext.getRequest().getSession(); //得到session 13 //操作 14 req.setAttribute("req", "write req.."); //往request里存值 15 session.setAttribute("session", "write session"); //住session里存值 16 session.getAttribute("session"); //从session里取值 17 resp.getWriter(); //得到response的 PrintWriter 18 return "test"; 19 } 20 }
依赖注入
1 //实现ServletRequestAware,ServletResponseAware接口 2 public class UserAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{ 3 private HttpServletResponse resp; 4 private HttpServletRequest req; 5 private HttpSession session; 6 private ServletContext app; 7 8 public String execute() throws IOException{ 9 //操作 10 req.setAttribute("req", "write req.."); //往request里存值 11 session.setAttribute("session", "write session"); //住session里存值 12 session.getAttribute("session"); //从session里取值 13 resp.getWriter(); //得到response的 PrintWriter 14 return "test"; 15 } 16 17 //重写setServletResponse方法,Response对象不需要自己得到,由struts2从外部注入 18 public void setServletResponse(HttpServletResponse resp) { 19 this.resp=resp; //得到response 20 } 21 //重写setServletRequest方法,Request对象不需要自己得到,由struts2从外部注入 22 public void setServletRequest(HttpServletRequest req) { 23 this.req=req; //得到request 24 this.session = req.getSession(); //得到session 25 this.app = session.getServletContext(); //得到application 26 } 27 }
非耦合
非依赖注入
1 public class UserAction extends ActionSupport{ 2 //得到的是由struts封装好的Map类型,只能对范围进行存取值操作 3 private Map<String,Object> req; 4 private Map<String,Object> session; 5 private Map<String,Object> app; 6 7 public String execute() throws IOException{ 8 req= (Map<String,Object>)ActionContext.getContext().get("request"); //得到request 9 session = ActionContext.getContext().getSession(); //得到session 10 app = ActionContext.getContext().getApplication(); //得到application 11 //操作 12 req.put("req", "write req.."); //存值到request 13 session.put("session", "write session.."); //存值到session 14 app.put("app", "write application.."); //存值到application 15 session.get("session"); //从session存值 16 return "test"; 17 } 18 }
依赖注入(推荐)
1 //实现RequestAware,SessionAware,ApplicationAware等接口 2 public class UserAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ 3 //得到的是由struts封装好的Map类型,只能对范围进行存取值操作 4 private Map<String,Object> req; 5 private Map<String,Object> session; 6 private Map<String,Object> app; 7 8 public String execute() throws IOException{ 9 //操作 10 req.put("req", "write req.."); //存值到request 11 session.put("session", "write session.."); //存值到session 12 app.put("app", "write application.."); //存值到application 13 session.get("session"); //从session存值 14 return "test"; 15 } 16 //得到application 17 public void setApplication(Map<String, Object> app) { 18 this.app=app; 19 } 20 //得到session 21 public void setSession(Map<String, Object> session) { 22 this.session=session; 23 } 24 //得到request 25 public void setRequest(Map<String, Object> req) { 26 this.req= req; 27 } 28 }