在Struts2中,经过IoC方式将Servlet对象主入到Action中,具体实现是由一组接口决定的。要采用IoC方式就必须在Action中实现如下接口:java
ApplicationAware:以Map类型向Action注入保存在ServletContext中的Attribute集合session
SessionAware:以Map类型向Action注入保存在HttpSession中的Attribute集合app
CookieAware:以Map类型向Action注入Cookie中的数据集合this
ParameterAware:向Action中注入请求参数集合spa
ServletRequestAware:实现该接口的Action能够直接访问HttpServletRequest对象,Action必须实现该接口的void setServletRequest(HttpServletRequest request)方法。code
ServletResponseAware:实现该接口的Action能够直接访问HttpServletResponse对象,Action必须实现该接口的void setServletResponse(HttpServletResponse response)方法。对象
ServletContextAware:实现该接口的Action能够直接访问ServletContext对象,Action必须实现该接口的void setServletContext(ServletContext context)方法。接口
public void setServletRequest(HttpServeltRequest request){ this.request=request;//获取request对象 this.session=request.getSession();//获取Session对象 this.application=session.getServletContext();//获取application对象 }