如何使用OGNL索引引用运算符 [英] How to use OGNL index reference operator

查看:81
本文介绍了如何使用OGNL索引引用运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用OGNL,您可以引用操作上下文对象,例如 #application #session #root #action #request #参数 #attr ,以及 #context 的操作上下文。

Working with OGNL you can reference action context objects like #application, #session,#root, #action,#request,#parameters,#attr, and the action context with #context.


框架将OGNL上下文设置为我们的ActionContext,并将
值堆栈设置为OGNL根对象。

The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object.

OGNL使用 [] 作为索引参考来访问对象属性。例如,如果对象 foo 具有属性 bar ,那么它可以像 foo.bar一样访问 foo ['bar'] 。如果 foo 是一张地图而且 bar 是一个关键字,它也有效。

And OGNL uses [] as index reference to access an object properties. For example if the object foo has a property bar then it can access like foo.bar or foo['bar']. It also works if foo is a map and bar is a key.

现在,我想将一个变量和一个值放到值堆栈上下文中

Now, I want to put a variable and a value to the value stack context like that

<s:set var="bar" value="'hello'"/>
<s:set var="foo" value="'bar'"/>

并打印价值

<s:property value="%{#attr[#foo]}"/>

它应打印 hello

我想知道这是如何工作的。我知道 #attr 是一个没有 #foo 引用的属性的对象,即。然而这是有效的。如果我使用 #request #context ,并且可能 #root 而不是 #attr 。这两个对象都没有属性 bar ,但是OGNL认为不是这样。我想知道OGNL对它引用的对象的属性的看法以及为什么这个表达式正在工作。此外,如果有其他方法可以在OGNL表达式中使用 #foo 引用打印 hello

I'd like to know how this works. I know that #attr is an object that doesn't have a property referenced by #foo, i.e. bar. However this works. It also works if I use #request and #context, and probably #root instead of #attr. Neither of this objects has a property bar, but OGNL thinks otherwise. I'd like to know what OGNL thinks about property of the object it references and why this expression is working. Also if there are alternative ways to print hello using #foo reference in OGNL expression.

推荐答案

在给定的表达式中< s:property value =#attr [#foo] /> 将首先评估 [] 内的部分。 #foo 已解析为 bar 因此表达式变为 #attr ['bar'] (相当于#attr.bar )。

In the given expression <s:property value="#attr[#foo]"/> the part inside [] will be evaluated first. The #foo is resolved to bar so expression becomes #attr['bar'] (which is equivalent to #attr.bar).

使用#attr.bar 将搜索 bar 的值,直到在页面上下文中找到它,然后在请求,然后在会话中,然后在应用程序范围内。

Using #attr.bar the value for bar will be searched until it is found in the page context, then in the request, then in the session and then in the application scope.

#context.bar 从OGNL上下文值映射中获取值,其中键为 bar

The #context.bar gets value from OGNL context value map with key bar.

#request.bar 尝试获取名称为 bar 如果找不到,那么将在值堆栈中搜索 bar 。这发生在Struts2请求包装器实现中。

The #request.bar tries to get request attribute with name bar from the request map and if it isn't found then bar will be searched in the value stack. This happens in Struts2 request wrapper implementation.

这篇关于如何使用OGNL索引引用运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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