ActionContext和ServletActionContext区别

1.ActionContext
xwork框架的ActionContext是Action执行时的上下文,存放Action执行时需要用到的对象。在使用webwork时,其中放有Parameter、Session、ServletContext、Locale等信息。这样,webwork负责将Servlet相关数据转换为与ServletAPI无关的Map对象(即ActionContext),使得xwork的Action实现与web层、逻辑层与表现层的解耦。

2.ServletActionContext
提供直接与Servlet容器交互的途径。通过它,可以取得HttpServletRequest、HttpServletResponse 、ServletConfig、ServletContext、PageContext 对象。但是,使用ServletActionContext意味着Action与ServletAPI的紧密耦合。

3.由于WebWork对request,parameter,Session和Application都进行了封装,将这些隐含的对象封装成了相应的Map,如RequestMap,ParameterMap,SessionMap和ApplicationMap,而这些Map就组成

了ActionContext,因此我们通常都不再需要与request,session这些底层的对象打交道了,这也是我一开始觉得迷惑的地方,因为我找不到Session了。事实上,对于SessionMap的处理即是对Session的

处理了。我们可以通过ActionContext的静态方法getContext返回一个ActionContext的实例,然后再调用其getSession方法获得SessionMap,接着就可以利用put和get方法对session进行读写的操作了。


Map params = new HashMap();;  
params.put(String, String);;  
......  
Map extraContext = new HashMap();;  
extraContext.put(ActionContext.PARAMETERS,params);

Map session = new HashMap();;  
session.put("foo", "bar");;  
extraContext.put(ActionContext.SESSION, session);

ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
session.put("username",loginInfo.getUsername());

ActionContext ctx = ActionContext.getContext();
Map params = ctx.getParameters();
String username = ctx.getParameters("username");

OgnlValueStack stack = new OgnlValueStack();
stack.push(new User());//首先将欲赋值对象压入栈中
stack.setValue("name","erica");//为栈顶对象的指定属性名赋值

猜你喜欢

转载自yimengdaotianming.iteye.com/blog/1206722
今日推荐