日期:2014-05-18  浏览次数:20640 次

ActionContext类有这个getContext()方法,怎么方法后面有有方法啊,这是什么逻辑?
public String execute() throws Exception{

Map request = (Map)ActionContext.getContext().get("request");

request.put("list", service.findAll());

return SUCCESS;

}

这里面的ActionContext.getContext().get("request")怎么解释?ActionContext类有这个getContext()方法,怎么方法后面有有方法啊,这是什么逻辑?

------解决方案--------------------
struts2的request session 都封装在Map集合里,ActionContext.getContext().get("request")是指先得到上下文信息,在得到request对象 IOC有两种
------解决方案--------------------
ActionContext.getContext()返回值是一个对象A 对象A.get("request")这是一个方法。
连起来写就是ActionContext.getContext().get("request");

------解决方案--------------------
学习下
------解决方案--------------------
Person{//"人"类
Mouth yourMouth = new Mouth();
void getMouth(){//得到嘴
return yourMouth;
}
}

Mouth(){//"嘴"类
void eat(){//“吃"方法
.....
}
}
Person p = new Person();
Mouth m = p.getMouth();//得到嘴巴这个对象m
m.eat();//执行对象“嘴巴”的方法eat
p.getMouth().eat();//上面两句合起来
------解决方案--------------------
不好意思写错了。
1,Person类中的方法getMouth改为Mouth getMouth.此方法返回Mouth对象,不是void
2,Mouth类后面的括号去掉

------解决方案--------------------
引用:
明白点了,我说一下你们看看我说的对不:就是说ActionContext类里声明了context类,在Context()类里面有get方法,是这样吗?


正是。
context类中的get方法得到的又是一个对象。。这个对象就可以执行它的自己的方法,比如get("request");

------解决方案--------------------
接分的来了
------解决方案--------------------
看来LZ明白了
------解决方案--------------------
引用:
谢谢各位,我现在想问是怎么知道在Context()类里有get方法的,这是怎么查找的?

去查下api吧
------解决方案--------------------
ActionContext.getContext()返回的是一个ActionContext对象,get(String key)也是ActionContext里面的一个方法。
xwork源码贴出来了:
public class ActionContext implements Serializable {
    static ThreadLocal actionContext = new ThreadLocal();

 
    //public static final String DEV_MODE = "__devMode";


    public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";

   
    public static final String VALUE_STACK = ValueStack.VALUE_STACK;

   
    public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";

    
    public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";


    public static final String PARAMETERS = "com.opensymphony.xwork2.ActionContext.parameters";


    public static final String LOCALE = "com.opensymphony.xwork2.ActionContext.locale";