aspose file tools*
The moose likes Struts and the fly likes How to access static fields using OGNL in Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to access static fields using OGNL in Struts 2" Watch "How to access static fields using OGNL in Struts 2" New topic
Author

How to access static fields using OGNL in Struts 2

Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
Hello,

I am having a class that contains the "public static final x" variable. How can I use it into a OGNL expression in a web page using Struts 2 tags?
I would like to achieve smth like this:



Thank you in advance.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

Your class will still need to have a getter method for the variable and the getter method cannot be static.

So if we have the following in our class:


In our JSP we would test this way:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That is not correct; static variables are accessible just fine.

http://struts.apache.org/2.x/docs/ognl-basics.html#OGNLBasics-Accessingstaticproperties
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

What David said is correct.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213



That is not correct; static variables are accessible just fine.

Doh! Your right!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Actually, it doesn't say anything about not being able to access static variables on that page. What it *does* say is that the *method* can't be static, but I don't think that's correct either: if you're configured to allow static access and use the OGNL static access syntax, it'll work. It's okay if you don't believe me, though.

Also, when linking to the online docs, make sure you link to (a) the most recent version (or version-specific when the version is known), and (b) not the cwiki.apache.org site, but rather the struts.apache.org site. Thanks.
Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
Hello,

Thanks for posting. The solutions were very good. All of them worked.

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access static fields using OGNL in Struts 2
 
Similar Threads
OGNL fixing precedence with ()
How to compare a Short with 1 in OGNL (Struts 2)
Using Struts to iterate over queries with aggregates
Unable to test property value in <s:if> tag
[Struts 2] Use a JSP variable in struts2 tag