`
hengxing2009
  • 浏览: 88620 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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出来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);
分享到:
评论

相关推荐

    webwork的使用教程

    它封装了当前请求的所有相关信息,如session、request、response对象以及值栈等。ActionContext允许你在Action之间共享数据,也可以通过它访问Servlet容器的相关服务。 **ServletActionContext** ...

    WebWork教程(初学者的学习)

    ActionContext是WebWork中的一个重要组件,它封装了当前请求的环境信息,如Session、Request、Response对象以及本地化信息。ActionContext提供了访问和设置这些上下文数据的方法。 - **ServletActionContext**:在...

    webwork学习

    - **介绍**:ActionContext是Action执行过程中所需的各种信息的容器,如Request、Response、Session等。 - **ServletActionContext**:特定于Servlet容器的ActionContext实现。 - **ServletDispatcher原理**:...

    Struts 2 技术详解:基于WebWork核心的MVC开发与实践

    2. **ActionContext**:它持有当前请求的上下文信息,如参数、session、request、response等。 3. **配置管理器**:通过XML配置文件或注解来定义Action的映射、拦截器链以及结果视图。 4. **Interceptor(拦截器)...

    struts小应用事例学习

    7. **Action上下文(ActionContext)**: 在请求处理过程中,ActionContext保存了当前请求的所有相关信息,如Action实例、值栈、Session、Request和Response对象等。开发者可以利用ActionContext访问这些上下文对象,...

    struts2学习心得

    例如:public class HelloWordAction extends Action{public ActionForward execute(ActionMapping mapping, ActionForm form, HttpRequest request, HttpResponse response)throws Exception{。。。。}} 而Struts2...

    javaWeb系列 serverlet最简单登陆代码

    Struts是一个MVC框架,它提供了Action类来处理请求,而Hibernate则是一个ORM(对象关系映射)框架,用于与数据库交互。Struts2是Struts1的升级版,结合了Struts1和WebWork的优点,其Action类和拦截器使得处理逻辑...

    struts学习笔记

    - `ServletActionContext`可用于获取`request`、`response`和`servletContext`对象,但不提供直接获取`session`的方法。 #### 三、Struts.xml中package标签的详细配置说明 **1. 基本属性**: - **name**:包的...

    开发框架学习

    在JSP或Servlet中,通常通过`request.getParameter()`方法获取表单数据。如果要获取名为"name"的表单字段的值,应使用`request.getParameter("name")`。题目中的第9题也涉及了这一知识点。 **页面重定向和转发**: ...

    struts-2.5.jar

    9. **Action上下文(ActionContext)**:ActionContext封装了当前请求的所有相关信息,包括参数、session、request和response对象,便于Action访问。 10. **注解支持**:Struts 2支持使用注解来简化配置,如`@...

    struts2工程核心包

    5. **ActionContext**:封装了当前请求的上下文信息,如Session、Request、Response等,提供了一种在Action之间共享数据的途径。 6. **插件支持**:核心包还提供了对插件的管理和加载,使得可以方便地扩展框架功能...

Global site tag (gtag.js) - Google Analytics