论坛首页 Java企业应用论坛

使用ValueStack或者ActionContext?

浏览 18226 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-03-16  
以前这样的操作: request.setAttribute("list",List); 在webwork中应该等同于下面哪种情况?
1.ActionContext ac=ActionContext.getContext();
ac.getContextMap().put("list",List);

2.ac.put("list",List);

3.ac.getValueStack().setValue("list",List);

在使用ActionContext的put方法存取变量的时候,他是否把变量存放到request中? 但是我在jsp页面上用 request.getAttribute得不到这个list变量,webwork把它存放到哪里了?

如果用
ac.getValueStack();.setValue("email","lyo@lyo.com");;

设置一个email变量到ValueStack中,然后用
ac.getValueStack();.findValue("email");;

确得不到这个变量!  用法有问题么?

用ValueStack和用ActionContext的put,哪个比较好?
   发表时间:2005-03-16  
应该是
Map requestAttributes = (Map);ActionContext.get("request");;
0 请登录后投票
   发表时间:2005-03-16  
z_jordon 写道
应该是
Map requestAttributes = (Map);ActionContext.get("request");;


哦,多谢. 如果这样设置变量,

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

requestAttributes.put("name","yahoo");;

我在页面上就应该 这样得到:<ww:property value="#request['name']"/>或者这样:
:<ww:property value="#attr['name']"/>
如果这样设置的变量:
ac.put("list",List);
那麽我在页面上应该用:<ww:property value="#attr['name']"/>
可以得到的,但事实上不可以,必须用:<ww:property value="#list"/>
才可以.

你认为用  (Map)ActionContext.get("request");
和使用getValueStack()哪个更好呢?
1 请登录后投票
   发表时间:2005-03-16  
引用

我在页面上就应该 这样得到:&lt;ww:property value="#request['name']"/&gt;或者这样:
:&lt;ww:property value="#attr['name']"/&gt;

通过#request['name']就相当于request.getAttribute("name");
而通过#attr['nmae']就相当于依次调用pageContext,request.session,servletContext的getAttribute("name")方法,所以第一种的效率会比较高一些。
引用

如果这样设置的变量:
ac.put("list",List);
那麽我在页面上应该用:&lt;ww:property value="#attr['name']"/&gt;
可以得到的,但事实上不可以,必须用:&lt;ww:property value="#list"/&gt;
才可以.
你认为用 (Map)ActionContext.get("request");
和使用getValueStack()哪个更好呢?

看WebWork的源码你就会发现,ActionContext这个类中有一个变量:
Map context,
实际上调用ActionContext的put,get 就相当于调用context的相就方法,你在页面上通过#request,#attr获取的就相当于调用context.get("request"),context.get("attr"),而你通过ac.put("list", List)也只能通过ac.get("list")获得。
而ActionContext.getValueStack()相当于调用context.get(OgnlValueStack.VALUE_STACK)所获取的与通过ActionContext.get("request")获取的是不一样的东西,所以我觉得这不存在什么好与不好的问题。
1 请登录后投票
   发表时间:2005-03-18  
多谢指点,但是按照上面的说法,我在页面上传给后台Servlet一个参数(例如Servlet?id=43),我在Action中使用
Map parammap= (Map);ActionContext.get("request");; 
parammap.get("id");;


这样得到的id却是null,不知为何.  这个id难道不是存在request中么?

ValueStack也是吧对象压入内存,你能举个使用这种方法的典型例子么?我不是很清楚他和我把变量存入 request,application 中的区别.多谢!
1 请登录后投票
   发表时间:2005-03-18  
lyo 写道
多谢指点,但是按照上面的说法,我在页面上传给后台Servlet一个参数(例如Servlet?id=43),我在Action中使用
Map parammap= (Map);ActionContext.get("request");; 
parammap.get("id");;


这样得到的id却是null,不知为何.  这个id难道不是存在request中么?

通过ActionContext.get("request")的Map实例是对request.getAttribute(...),request.setAttribute(..)的封装,所以你这样是得不到你想要的结果的,
这时候你应该通过(Map)ActionContext.get("parameters")来获取你想要的结果,这才是对request.getParameter(...)的封装。
事实上,这些东西可以通过看源码来解决问题,webwork的源码挺好看的。
引用

ValueStack也是吧对象压入内存,你能举个使用这种方法的典型例子么?我不是很清楚他和我把变量存入 request,application 中的区别.多谢!

这个问题我恐怕不能够给你满意的答复了,呵呵,我也 正在学习webwork,我只知道ValueStack的使用是为了在页面上支持表达式语言,在webwork中一般是把一些Action压入ValueStack的,其它的, 我能力有限,表达能力也不够,希望有热心人帮忙解析清楚。
1 请登录后投票
   发表时间:2005-03-21  
多谢,  在 opensymphony 上,joson这样回复的:

引用

The value stack is good for setting properties on your Action or other objects you've pushed onto the stack. The ActionContext holds the value stack but also holds a loto f other things. You can't set random values on the value stack, it's going to try to find a property with that name on the Action. The ActionContext is essentially a Map that's set as a ThreadLocal.


好像大体的意思也是效率问题,不知道大家有没有更准确的看法?
0 请登录后投票
   发表时间:2005-03-22  
lyo 写道
多谢指点,但是按照上面的说法,我在页面上传给后台Servlet一个参数(例如Servlet?id=43),我在Action中使用
Map parammap= (Map);ActionContext.get("request");; 
parammap.get("id");;


这样得到的id却是null,不知为何.  这个id难道不是存在request中么?

ValueStack也是吧对象压入内存,你能举个使用这种方法的典型例子么?我不是很清楚他和我把变量存入 request,application 中的区别.多谢!


你的Servlet?id=43指向自己的servlet而不是webwork的servletDispatcher,又怎能执行action呢
0 请登录后投票
   发表时间:2005-03-22  
lyo 写道
以前这样的操作: request.setAttribute("list",List); 在webwork中应该等同于下面哪种情况?
1.ActionContext ac=ActionContext.getContext();
ac.getContextMap().put("list",List);

2.ac.put("list",List);

3.ac.getValueStack().setValue("list",List);

在使用ActionContext的put方法存取变量的时候,他是否把变量存放到request中? 但是我在jsp页面上用 request.getAttribute得不到这个list变量,webwork把它存放到哪里了?

如果用
ac.getValueStack();.setValue("email","lyo@lyo.com");;

设置一个email变量到ValueStack中,然后用
ac.getValueStack();.findValue("email");;

确得不到这个变量!  用法有问题么?

用ValueStack和用ActionContext的put,哪个比较好?


得不到email变量的值的原因是,当前ValueStack上下文中没有发现属性名为email的属性或当前的Action没有添加setEmail(String email)方法,因此ac.getValueStack().setValue("email","lyo@lyo.com")无效;
1 请登录后投票
   发表时间:2005-03-22  
要不然你认为楼主应该怎么表达呢?
引用

你的Servlet?id=43指向自己的servlet而不是webwork的servletDispatcher,又怎能执行action呢
1 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics