访问web元素的四种方法(耦合,依赖注入)、(耦合,非依赖注入)、(非耦合,依赖注入)、(非耦合,非依赖注入)

耦合:可以得到HttpServletResponse,HttpServletRequest,HttpSession等类型对象。能直接操作到真正的web元素。

非耦合:得到的是由struts封装好的Map类型,只能对范围进行存取值操作。

依赖注入:对象由struts2从外部注入。Action类不需要关心对象是如何得到的。

非依赖注入:对象由Action类自己提供初始化。

耦合

  非依赖注入

 public class UserAction extends ActionSupport{
//类型为真实的web元素:HttpServletResponse,HttpServletRequest等..
private HttpServletResponse resp;
private HttpServletRequest req;
private HttpSession session;
private ServletContext app; public String execute() throws IOException{
req = ServletActionContext.getRequest(); //得到request
resp = ServletActionContext.getResponse(); //得到response
app = ServletActionContext.getServletContext(); //得到application
session = ServletActionContext.getRequest().getSession(); //得到session
//操作
req.setAttribute("req", "write req.."); //往request里存值
session.setAttribute("session", "write session"); //住session里存值
session.getAttribute("session"); //从session里取值
resp.getWriter(); //得到response的 PrintWriter
return "test";
}
}

  依赖注入

 //实现ServletRequestAware,ServletResponseAware接口
public class UserAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
private HttpServletResponse resp;
private HttpServletRequest req;
private HttpSession session;
private ServletContext app; public String execute() throws IOException{
//操作
req.setAttribute("req", "write req.."); //往request里存值
session.setAttribute("session", "write session"); //住session里存值
session.getAttribute("session"); //从session里取值
resp.getWriter(); //得到response的 PrintWriter
return "test";
} //重写setServletResponse方法,Response对象不需要自己得到,由struts2从外部注入
public void setServletResponse(HttpServletResponse resp) {
this.resp=resp; //得到response
}
//重写setServletRequest方法,Request对象不需要自己得到,由struts2从外部注入
public void setServletRequest(HttpServletRequest req) {
this.req=req; //得到request
this.session = req.getSession(); //得到session
this.app = session.getServletContext(); //得到application
}
}

非耦合

  非依赖注入

 public class UserAction extends ActionSupport{
//得到的是由struts封装好的Map类型,只能对范围进行存取值操作
private Map<String,Object> req;
private Map<String,Object> session;
private Map<String,Object> app; public String execute() throws IOException{
req= (Map<String,Object>)ActionContext.getContext().get("request"); //得到request
session = ActionContext.getContext().getSession(); //得到session
app = ActionContext.getContext().getApplication(); //得到application
//操作
req.put("req", "write req.."); //存值到request
session.put("session", "write session.."); //存值到session
app.put("app", "write application.."); //存值到application
session.get("session"); //从session存值
return "test";
}
}

  依赖注入(推荐)

 //实现RequestAware,SessionAware,ApplicationAware等接口
public class UserAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
//得到的是由struts封装好的Map类型,只能对范围进行存取值操作
private Map<String,Object> req;
private Map<String,Object> session;
private Map<String,Object> app; public String execute() throws IOException{
//操作
req.put("req", "write req.."); //存值到request
session.put("session", "write session.."); //存值到session
app.put("app", "write application.."); //存值到application
session.get("session"); //从session存值
return "test";
}
//得到application
public void setApplication(Map<String, Object> app) {
this.app=app;
}
//得到session
public void setSession(Map<String, Object> session) {
this.session=session;
}
//得到request
public void setRequest(Map<String, Object> req) {
this.req= req;
}
}
05-06 06:43