Struts中,IoC将Servlet注入到Action

在Struts2中,经过IoC方式将Servlet对象主入到Action中,具体实现是由一组接口决定的。要采用IoC方式就必须在Action中实现如下接口:java

  1.      ApplicationAware:以Map类型向Action注入保存在ServletContext中的Attribute集合session

  2.      SessionAware:以Map类型向Action注入保存在HttpSession中的Attribute集合app

  3.      CookieAware:以Map类型向Action注入Cookie中的数据集合this

  4.      ParameterAware:向Action中注入请求参数集合spa

  5.      ServletRequestAware:实现该接口的Action能够直接访问HttpServletRequest对象,Action必须实现该接口的void setServletRequest(HttpServletRequest request)方法。code

  6.      ServletResponseAware:实现该接口的Action能够直接访问HttpServletResponse对象,Action必须实现该接口的void setServletResponse(HttpServletResponse response)方法。对象

  7.      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对象
}
相关文章
相关标签/搜索