问题
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