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

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

问题描述

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

在下面的代码中,var grpIndex 计算正确,例如,'6''7' 在我的例子中,并打印在下一行正确输出.以下语句(第 3 行)的计算结果也正确,例如,在我的情况下为 'stage''prod'.

但是,在第四行中,我尝试将相同的表达式设置为变量,使用与前一行相同的语法,以便我可以在标题中使用它.为了测试这个作业,我尝试在下一行打印出 var.我希望最后一行打印与第三行相同的值.然而,这个表达式什么也没打印出来.这是为什么?

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 的 Expression EvaluationLanguage Gude,你会发现如果你使用括号前没有点的括号表达式,则表达式的计算结果将作为要计算的表达式.请参阅此处的示例.

其次,我没有看到您获得迭代器索引以外的索引的原因.

第三,set 标记将变量放置到可通过 # 符号访问的值堆栈上下文.在此处查看示例.

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

因为第四行有错别字.用

替换该行

会做预期的事情.你可以看到表达上的差异.

最后一个只是为了向您展示应该在哪里使用 %{} 来强制计算表达式的参考,因为 Struts 正在解析 OGNL 标签属性中的所有内容(默认情况下).

<块引用>

%{} 符号的用法:

在 OGNL 中用于强制将括号中的内容计算为 OGNL 表达式.

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.

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.

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"/>

解决方案

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.

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.

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.

The usage of %{} notation:

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

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

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