What are different ways to access variables with OGNL in Struts 2

女生的网名这么多〃 提交于 2021-01-29 07:02:49

问题


I am working now in a Struts2 project which uses OGNL. I see three different ways to access data in JSP using OGNL.

value1 ="previousList"
value2 = "#previousList"
value3 = "%{previousList}"

What these will do and are there other ways to access data from OGNL?


回答1:


The value stack which is an implementation of ValueStack has two methods push and set. The first method pushes the variable to the stack, but the second sets in to the value stack's context. If the variable in the value stack's context, you can use different ways to access it. Struts has a feature if it can't find a variable in the value stack it searches the value stack's context.

<s:property value="previousList"/>
<s:property value="#previousList"/>
<s:property value="%{previousList}"/>

So, all of them print the value, but second case is a bit faster because it points out OGNL using # to find the value directly in the OGNL context. See more about OGNL in Struts docs.

On the other hand if the variable is not in the context but in the value stack's root the second method fails to return the value.

And the last point is that Struts parses its some tag attributes (almost all), like value, for OGNL, and %{} defines the scope of OGNL expression.



来源:https://stackoverflow.com/questions/32543346/what-are-different-ways-to-access-variables-with-ognl-in-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!