webwork获取request,response的方法

ServletActionContext.getRequest  获得request
ActionContext.getContext().getSession() 获得session

ActionContext就是它,封装了些内置对象,很好!

以下还有详细的:

webwork Action中获取request, response对象的方法
import com.opensymphony.xwork.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;
ActionContext ctx = ActionContext.getContext();

    HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);

    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
    //ServletActionContext.APPLICATION;
    //ServletActionContext.SESSION;
    //ServletActionContext.PAGE_CONTEXT;

在webwork2的action里取request.getParameter参数

webwork的action已经脱离的request,是用getXxx()来取提交过来的参数
如果在写程序的时候特定需要自己来取Parameter可以通过以下两种方法实现

第一种用ActionContext类,所有的参数都从这里ActionContext.getContext().getParameters()取
他返回的是一个Map类型
Map param= ActionContext.getContext().getParameters();
如果有一个提交过来的username
那就可以这样写
param.get("username"); 不过这里要注意一下param.get("username")是一个String数组(为什么要返回数据我也不知道,我从weblogic窗口看到 param.get("username")被out出来Ljava.lang.String,忙活了半天)

    String value[] = (String[])param.get("username");
    String username = "";
    for(int i=0;i<value.length;i++)
    {
     username +=value[i];
    }
这样就可以得到正确的username了

第二种方法是直接把request引用进来

ServletActionContext.getRequest().getParameter("username")
ServletActionContext.getRequest()就是httpservletrequest
这个类再import com.opensymphony.webwork.ServletActionContext
用起来方便些
webwork的action已经脱离的request,是用getXxx()来取提交过来的参数
如果在写程序的时候特定需要自己来取Parameter可以通过以下两种方法实现

WebWork之Session ---------

由 于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进行读写的操作了。
          而在页面上,我们可以通过以下的方式对session进行操作:
<webwork:property value="#session.name" />
          #session.name表示从SessionMap中取得与"name"这个key对应的对象,实际上是调用了如下的 statement:ActionContext.getContext().getSession().get("name"),并且进行了类型的转 换。又如:      
<webwork:property value="#session.player.name" />
          则是在SessionMap中获得了Player对象之后,并调用类Player的getter方法:getName()获得name属性。
          简而言之,为了能够降低与部署环境的耦合程度,WebWork将Servlet的隐含对象进行了封装,这在很大程度上简化了开发的工作。而且 WebWork也提供了类ServletActionContext,我们通过这个类中的getRequest方法获得原始的 HttpServletRequest,然后就可以对request和session这些底层对象进行操作了。但是,一般情况下,利用 ActionContext.getSession()可以完成几乎所有的工作了,我们又为什么要去碰那些底层的东西呢?因此我们应该优先考虑使用 SessionMap,而不是底层的session。
          另外一个需要注意的问题,就是SessionMap和隐藏对象session的作用域是不同的。也就是说,通过 ActionContext.getContext().getSession().put("name","Fantasy Soft"),往SessionMap中写入了与"name"这个key相对应的内容,但是在页面上通过 session.getAttribute("name")得到的将会是null。


访问Application,session,request 对象

Trim by Alex.Sue

Webwork提供了很多的方法来访问Session,Application,Request。

Eg:

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

session.put("myId",myProp);

或者:

ServletActionContext.getRequest().getSession()

Note:不要在action的构造函数里调用ActionContext.getContext()。可能因为值未设置而返回的是null值。

Note Also:ActionContext.getContext().get(“session”)=(Map)

ActionContext.getContext().getContext.getSession().

如果需要访问HttpSession,可以通过ServletConfigInterceptor接口。

在视图层中,JSP可以通过以下的方式访问:

<ww: property value="#session.myId" />

<ww: property value="#request.myId" />

所有的servlet范围可以这样的访问:

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

request.put("myId",myProp);

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

application.put("myId",myProp);

Map session = (Map) ActionContext.getContext().get("attr");

attr.put("myId",myProp);

猜你喜欢

转载自douglaslau.iteye.com/blog/1403243