为什么可以有Map<String,Object> application=ActionContext.getContext().getApplication();
Map<String,Object> session=ActionContext.getContext().getSession();
但是却没有ActionContext.getContext().getRequest();为什么不提供这个方法,而是ActionContext.getContext().get("request"),Struts2为什么这样设计?求教

解决方案 »

  1.   

    ActionContext.getContext().get("session")应该也是可以的,给session、application提供了专门的操作方法,其他的都是用get(“name”)来获取,这样简化了接口方法,context对象可以获取很多对象,没有必要一一提供一个操作方法,所以使用get这样一个公共的方法来操作。为什么这样设计就得的问作者了。
      

  2.   

    Struts 2.0中com.opensymphony.xwork2.ActionContext类。我们可以通过它的静态方法getContext()获取当前Action的上下文对象。 另外,org.apache.struts2.ServletActionContext作为辅助类(Helper Class),可以帮助您快捷地获得这几个对象。 HttpServletRequest request = ServletActionContext.getRequest(); 
    HttpServletResponse response = ServletActionContext.getResponse(); 
    HttpSession session = request.getSession();
    如果你只是想访问session的属性(Attribute),你也可以通过ActionContext.getContext().getSession()获取或添加session范围(Scoped)的对象。
      

  3.   

    从方法的返回值就能够看出,你只是定义了一个名字是application的map类变量而已...那个get方法主要是用于获取对应的属性值
    而其实并未拿到对应的context
    http的那个context肯定有你想用的那个方法
    那个是servlet里的东西.
    大概就是这么理解的吧
    如果按照你的想法,getrequest()方法也可以继续使用的话,那么request和session和application的scope不就相当了么.那么肯定还会有getpagecontext的方法咯.你觉得那个合理么?