当前位置:文档之家› webwork Action中获取request, response,session对象的方法

webwork Action中获取request, response,session对象的方法

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出来https://www.doczj.com/doc/1f15255751.html,ng.String,忙活了半天)

String value[] = (String[])param.get("username");
String username = "";
for(int i=0;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进行操作:

#https://www.doczj.com/doc/1f15255751.html,表示从SessionMap中取得与"name"这个key对应的对象,实际上是调用了如下的stateme

nt:ActionContext.getContext().getSession().get("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可以通过以下的方式访问:





所有的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);

相关主题
文本预览
相关文档 最新文档