Monday, October 27, 2008

Forcing OGNL - struts 2

I wanted to develop a dynamic menu with some set of links by retrieving appropriate .action names from a database table. It was a tricky point for me to set the 'action' attribute of the <s:url> tag. The property name that I wanted to set to 'action' attribute was 'actionName' which is already in the 'ValueStack'. Because, 'actionName' is already in the 'ValueStack', first I tried in this manner

<s:url action="actionName" id="myUrl">
    <s:param name="userId" value="1001"></s:param>
</s:url>

<a href="'<s:property  value="#myUrl">'">
    Click me
</a>

This was completely wrong syntax. Because here, 'actionName' is evaluated as a String itself, it does not retrieve the 'actionName' from the 'ValueStack'. Simply It generates the fallowing HTML mark up, which was not expecting.

<a href="/pda/acionName.action?userId=1001">
   Click me
</a>

To get the value for 'actionName' from the 'ValueStack', we need to force OGNL as fallows. We need to use %{expresion} notation to force OGNL to get the property of 'actionName' from the 'ValueStack' as a String literal.

<s:url action="'%{actionName}''" id="myUrl">
     <s:param name="userId" value="1001"></s:param>
</s:url>

<a href="'<s:property" value="#myUrl"> Click me </a>

This created the expected HTML mark up as fallows.

<a href="/pda/updateUser.action?userId=1001">
   Click me
</a>

* pda - is my context path

* updateUser - is one of my action name

0 comments:

Post a Comment

Share

Widgets