Question

I have been given the following code of a JSP page that uses the Struts2 framework

<s:form id="crud" action="crudmerge" namespace="templates">
    <s:hidden name="crudMode" />
    <s:hidden name="item.id" />    
    ...    
    <s:if test="crudMode == 1">             
            ...             
    </s:if>
    <s:else>
        <s:if test="%{version != null}">                
            ...             
        </s:if>
        <s:else>                
            ...             
        </s:else>
    </s:else>
</s:form>

in the action class that results in this JSP page, there is the following methods (among others): getCrudMode(), getItem(), and item has a method called getId().

I understand how struts2 will use those methods to fill in the <s:hidden> tags. I'm having problems however with the following line:

 <s:if test="%{version != null}">

First, I thought this worked the same as the other two, and Struts2 would run the getVersion() method of the action class. However, there is no field called version and no method getVersion(). There is also no <s:set name="version"> variable tag in the JSP. What could possibly be the point of this code? Are the %{} really necessary?

It is possible this is legacy code that needs to be removed, but I'm not sure what the difference is between the first two crudmode and item.id and the latter %{version}.

Was it helpful?

Solution

In the test attribute of the s:if tag you don't need to put %{}. This abbreviation is used to mark the scope of OGNL expression and removed before evaluation. While evaluating it's searching for variable version in the valueStack. The action object is on top of the valueStack, so if it finds a property with the name version it will execute getVersion() to return a result. If it didn't find the version at the action it will continue to search the whole valueStack until it find the property with the name version. More or like the same is described in the documentation.

XWork has a special OGNL PropertyAccessor that will automatically look at the all entries in the stack (from the top down) until it finds an object with the property you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top