近日在编写一个Struts2的拦截器的时候,需要对其中的参数进行二次处理,并将处理后结果在写入ValueStack中;之前采用了ValueStack的 setValue(string key,Object object)的方法,可是在前端的获取值的时候,出现问题:无法获取到写入ValueStack的值,调试的时候,采用 findValue(String key) 获取的也是一个null;
上网检查了 ValueStack的api,对于setValue方法的说明如下:
setValue
void setValue(String expr, Object value)
expr
- the expression defining the path to the property to be set.
value
- the value to be set into the neamed property
setValue是对值栈中的对象属性进行设置;key的语法与OGNL表达式的取值语法一样;
在API中,发现了另外一个方法 set(String key,Object object);
set
void set(String key, Object o)
key
-
o
-
检查了xwork的源码,发现 set(String key,Object object); 其实是在ValueStack的栈顶增加了一个HashMap,而后将key,Object 参数put到Map中;
由此可见;对于ValueStack设置值,如果是之前对象属性中没有的,应该采用set(String key,Object object)方法搞定;
调整之后的代码如下:
public void beforeResult(ActionInvocation arg0, String arg1) { //如果是下载; if(CoreConstants.FlexiGridDown.equalsIgnoreCase(this.actionType)){ //转入下载处理; //获取下载的信息; String jsonString = arg0.getStack().findString(CoreConstants.FlexiGridJsonString); //生成下载信息 InputStream inputStream = FlexiGridJsonToFile.flexGrid2Excel(jsonString, this.colModel); //写入ValueStack; arg0.getStack().set(CoreConstants.FlexiGridStream, inputStream); arg0.setResultCode(CoreConstants.FlexiGridDownResult); } }
通过Google大神,在CSDN上发现了一篇文章:“valuestack的工作原理 ” http://blog.csdn.net/pwlazy/article/details/2318494 ,另外提到了一些使用 set(String key,Object object) 的注意点 ,也很重要,而且很容易忽略。千万注意了。。。