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 [EMAIL PROTECTED]
<s:set value="@[EMAIL PROTECTED]" var="I"/>
008-05-02 00:36:18,747 WARN [http-8080-Processor23] [OgnlValueStack] Could not find property [EMAIL PROTECTED]@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="@[EMAIL PROTECTED]" 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 [EMAIL PROTECTED] 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="@[EMAIL PROTECTED]" 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="[EMAIL PROTECTED]"/> does not work. OGNL won't find a static field of an instance.

regards,
Jeromy Evans




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to