Struts2标签似乎评估不一致 [英] Struts2 tags seem to evaluate inconsistently

查看:57
本文介绍了Struts2标签似乎评估不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Struts2标记与OGNL混合使用的问题.为了克服无法在OGNL中嵌套表达式求值(例如%{foo[%{bar}]})的事实,我使用了对变量的连续赋值来获取所需的结果.

I have an issue with some Struts2 tags mixed with OGNL. To overcome the fact that you cannot nest evaluation of expressions in OGNL, e.g., %{foo[%{bar}]}, I use successive assignment to variables to get the results that I need.

在下面的代码中,变量grpIndex正确求值,例如我的情况下'6''7',并在下一行正确打印.以下语句(第3行)也可以正确求值,例如我的情况是'stage''prod'.

In the following code, the var grpIndex evaluates correctly, e.g., '6' or '7' in my case, and prints out correctly on the next line. The following statement (line 3) also evaluates correctly, e.g., 'stage' or 'prod' in my case.

但是,在第四行中,我尝试使用与上一行相同的语法将相同的表达式设置为变量,以便可以在标题中使用它.为了测试此分配,我尝试在下一行上打印出var.我希望最后一行打印与第三行相同的值.但是,此表达式不打印任何内容.为什么会这样?

However, in the fourth line I try setting that same expression into a variable, using the same syntax as the previous line, so that I can use it in a title. To test this assignment, I try to print out the var on the next line. I would expect the last line to print the same value as the third line. However, this expression prints out nothing. Why is this?

<s:set var="grpIndex" value="%{options[#optstatus.index]}"/>
grpIndex = <s:property value="#grpIndex"/><br/>
grpName = <s:text name="model[%{grpIndex}].groupName"/><br/>
<s:set var="grpName" value="model[%{grpIndex}]).groupName"/>
groupName = <s:property value="#grpName"/>

推荐答案

首先,您不能完全正确地说您不能在OGNL中嵌套表达式的求值.如果您查看OGNL的表达评估语言Gude,您会发现,如果使用带括号的表达式,但括号前没有点,则该表达式的求值结果将用作要求值的表达式.请参见此处的示例.

First, you are not completely correct saying that you cannot nest evaluation of expressions in OGNL. If you look at the Expression Evaluation of the OGNL Language Gude, you will find that if you use parenthesized expression without a dot in front of the parentheses the result of evaluating that expression will be used as expression to be evaluated. See the example here.

第二,我看不到您获得除迭代器索引以外的其他索引的原因.

Second, I don't see the reason for which you are getting indexes other than iterator index.

第三,set标记将变量放置到值堆栈上下文中,该上下文可通过#表示法进行访问.请参见此处的示例.

Third, the set tag places variables to the value stack context that is accessible via # notation. See the examples here.

第四,回答您的直接问题:

Fourth, answering your direct question:

因为第四行有错字.将行替换为

Because a typo in the fourth line. Replace the line with

<s:set var="grpName" value="model[%{#grpIndex}].groupName"/>

将完成预期的工作.您可以看到表情上的差异.

will do what expected. You can see the differences in expression.

最后一个参考只是向您显示应该使用%{}强制执行表达式的位置,因为Struts会解析OGNL标记属性中的所有内容(默认情况下).

The last is just reference to show you where you should use %{} to force evaluation of expression, because Struts is parsing everything (by default) in the tag attributes for OGNL.

%{}表示法的用法:

在OGNL中用于强制将括号中的内容作为OGNL表达式进行评估.

Used in OGNL to force evaluate the content in brackets as OGNL expression.

这篇关于Struts2标签似乎评估不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆