2019独角兽企业重金招聘Python工程师标准>>>
在Struts2中,通过IoC方式将Servlet对象主入到Action中,具体实现是由一组接口决定的。要采用IoC方式就必须在Action中实现以下接口:
ApplicationAware:以Map类型向Action注入保存在ServletContext中的Attribute集合
SessionAware:以Map类型向Action注入保存在HttpSession中的Attribute集合
COOKIEAware:以Map类型向Action注入COOKIE中的数据集合
ParameterAware:向Action中注入请求参数集合
ServletRequestAware:实现该接口的Action可以直接访问HttpServletRequest对象,Action必须实现该接口的void setServletRequest(HttpServletRequest request)方法。
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对象
}