Tuesday, October 28, 2008

OGNL - Calling Static Methods and Fields - struts 2

Most of the time, we have used to declare system constants as static fields
of a class, and some time static methods are being involved. If we want to access this kind of field from a JSP page with struts 2 taglibs, we have tow methods. I have tow classes 'SystemConstants' and 'Utils' in my own application.

public class
SystemConstants{
// ............
public static final
SESSION_DATA = "sessionData";
// .........
}

Also I have a separate class called "SessionData" to keep the data needed for the entire session. Useful information needed to keep through out the session are stored in the "
SessionData".

public class
SessionData{

private User user ;

public setUser(User user){
this.user = user;
}

public User getUser(){
return this.user;
}

}
I have stored the SessionData object in the session object from an "Action" class of struts 2.
SessionData sd = new SessionData();
getServletRequest().getSession()
.setAttribute(SystemConstants.
SESSION_DATA, sd);

Now, I want to access the session object and the "
SESSION_DATA" static field from the jsp page. This is where the point comes. For this, we have tow methods.


Method one:
To access the static field ,
<s:set name="sdKey"
value="@com.axiohelix.pda.utils.SystemConstants@
SESSION_DATA"></s:set>

Then to access the session object
from the "ActionContext",
<s:set name="objSd" value="#session[#sdKey]"/>

Then we can access the fields and methods declared in the SessionData object.
<s:property value="#objSd.user.userName"/>

This is how, we access static methods
@
com.axiohelix.utils.Utils@getSessionId()

If our field or method will resole in the 'ValueStack' in advance, we can use the fallowing syntax to access relevants
Method tow:
@vs@
SESSION_DATA
@vs@getSessionId()

The SESSION_DATA static field is declared in the class 'SystemConstants' packaged at
'com.axiohelix.utils' and getSessionId() function is implemented within the Utils class packaged at 'com.axiohelix.utils'


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
Share

Widgets