教学笔记-Struts2中访问web元素

 访问Web元素:request,session,application即对应的类HttpServletRequest,HttpSession,ServletContext

  *方式一:
  依赖于容器获取Map类型的request,session,application
<>在action中定义:private Map request;
 private Map session;
 private Map application;
<>可以在构造器里面或者其他地方用一下方法获取这三个元素:
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
 [Debug]里面的Action Context中可以查看到
<>我们获取的虽然都是Map,但是我们通过put方法将键值对存入Map之后,struts2
 会帮助我们和HttpRequest对象建立起联系,将值存入HttpRequest对象  
<>前台页面访问形式:<s:property value="#request.key"/>
 如果用<s:property value="#attr.key"/>可访问这三个对象中的值
 
  *方式二:
    IoC获取Map类型的request,session,application
    IoC-Inverse of Control控制反转
    DI-Dependency Injection依赖注入
  <>action类中实现三个接口:RequestAware,SessionAware,ApplicationAware
   让action知道这三个对象的存在,并且要实现三个方法;
  2>在action中定义三个Map,加上范型:
  private Map(String,Object) request;
private Map session;
private Map application;
  *方式三:
    依赖于容器获取HttpServletRequest,HttpSession,ServletContext对象
  <>在Action类中声明三个属性:
  private HttpServletRequest request;
  private HttpSession session;
  private ServletContext application;
  <>用如下方法可以获得这三个对象:
  request = ServletActionContext.getRequest();
  session = request.getSession();
  application = session.getServletContext();
 
  *方式四:
  IoC获取HttpServletRequest,HttpSession,ServletContext对象
  1>Action类实现ServletRequestAware接口,实现接口中的方法
  2>在Action中定义:
  private HttpServletRequest request;
  private HttpSession session;
  private ServletContext application;

你可能感兴趣的:(struts2,request,session,ActionContext,web元素)