struts2 Servlet API 解耦
firepig 于 2008-06-13 20:00:10struts2提供了三种和servlet解耦的方式:
1: ActionContext 这个接口对应的ServletAPI是HttpServletRequest。其中有两个方法,get(),set()对于于HttpServletRequest的getAttribute(),setAttribute().使用方法为:
ActionContext.getContext().put(key,value);
ActionContext.getContext().get(key);
相当于:
HttpServletRequest.setAttribute(key,value);
HttpServletRequest.getAttribute(key);
另外还直接获得session. ActionContext.getContext().getSession();
2:是基于IOC的方法实现的,在Action中必须实现ServletRequestAware,ServletResponseAware,ServletSessionAware. 不推荐使用。
3:ServletActionContext继承了ActionContext。
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getPesponse();
HttpSession session = request.getSession();
总结:第二种方法因为要实现接口不推荐使用,一般如果要获得session,用第一种方法。但是ActionContext不能获得request和response所有要用第三种。。。
