• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access static fields using OGNL in Struts 2

 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What David said is correct.
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Doh! Your right!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic