struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeromy Evans <jeromy.ev...@blueskyminds.com.au>
Subject Re: Simplified usage of:@com.company.Constants.StaticCOnstants
Date Thu, 01 May 2008 15:21:39 GMT
ravi_eze wrote:
> hi,
>
> even this doesnt seem to be working.
>
> is it possible to load the constant class to the context and then display it
> form there.? or in that case it expects a setters and getters... or some way
> of getting this done.. the code is very bad to read 'cos every time i am
> using @com.company.... 
>
> any help???
>
> cheers,
> ravi 
>
>   
I have a solution, but first...
OGNL only provides access to static fields and static methods.  It 
cannot reference a static class, nor even an inner static class.

http://www.ognl.org/2.6.9/Documentation/html/LanguageGuide/apa.html

It's terrible that struts2 provides no feedback all for all those 
invalid ONGL expressions.  It only provides feedback for valid 
expressions referencing invalid properties.

<s:set value="@com.blueskyminds.web.test.Constants" var="C"/>
008-05-02 00:36:18,747 WARN  [http-8080-Processor23] [OgnlValueStack] 
Could not find property [@com.blueskyminds.web.test.Constants]
<s:set value="@com.blueskyminds.web.test.Constants@InnerClass" var="I"/>
008-05-02 00:36:18,747 WARN  [http-8080-Processor23] [OgnlValueStack] 
Could not find property [@com.blueskyminds.web.test.Constants@InnerClass]

However, there is a bearable solution / hack:

1. In your Constants class, create a public static singleton instance of 
itself.  Lets call it C.  (Remember, OGNL can reference static fields)
public static final Constants C = new Constants();

2. In your Constants class, create a static getter for each constant.  
Thank goodness for the IDE's auto-generation of getters.

public static final String TEST_CONSTANT1 = "test1";
public static String getTEST_CONSTANT1() {
   return TEST_CONSTANT1;
}

3. Get get static field C and put it into scope

<s:set value="@com.blueskyminds.web.test.Constants@C" var="C"/>

4. Access its properties using dot notation. 
<s:property value="#C.TEST_CONSTANT1"/>

Note: You can't access the static fields of an instance, so you can't 
use #C@TEST_CONSTANT1.  It likes a static property.

Tested in Struts 2.1.1 with OGNL 2.6.11.  Dumb. 

To summarize:

package com.blueskyminds.web.test;
public class Constants {

    public static final String TEST_CONSTANT1 = "test1";

    // hack for OGNL follows:
    public static final Constants C = new Constants();

    public static String getTEST_CONSTANT1() {
        return TEST_CONSTANT1;
    }
}

In the JSP:
<s:set value="@com.blueskyminds.web.test.Constants@C" var="C"/>
<s:property value="#C.TEST_CONSTANT1"/>

*Note: this is code for 2.1.  In 2.0, the s:set tag uses the id 
attribute instead of var (I think).
*Note again: <s:property value="#C@TEST_CONSTANT1"/> does not work. OGNL 
won't find a static field of an instance.

regards,
 Jeromy Evans




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message