• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access static variable directly from jsf page

 
Priti Shah
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class which declares number of static variables used in application. Now I want to use these values in my jsf page.

To do this I declared my class as a managed bean in faces-config.xml


Now when I try to access it in my jsf page using


I am not able to get the value.

"BGA_PROGRESS_STATE" is defined as a public static final and BGAUtility Class does not have any getter method.

So what is going wrong?

But when I try same thing with get method it works fine. Do we have to give get method for all static variables?

I believe this is very common practice to declare all static variables in one class and use them across the application.

Thanks
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes getters are required.

For your functionality I would suggest you to explore f:loadBundle tag.
It acts as a resource bundle and you can pick the static strings from the properties file instead of Java class.
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been my experience that statics aren't available to JSF. I had DB pulls from static calls and couldn't see that stuff until I made a backing bean method (non-static) accessors that called the static methods and made the data available. Probably because JSF uses introspection to find accessors and mutators for instance variables. This wouldn't work with statics as they belong to the class and not the instance.
 
Priti Shah
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I will then provide get methods for my static variables.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Priti Shah:
Thanks for the reply.

I will then provide get methods for my static variables.


This sounds like a hack to me and not consistant with good design principals. What are you using these static variables for? If it is just String messages or words on a page, I'd follow the advice of Varun and look into f:loadBundle to load String resources from a properties file.
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:


This sounds like a hack to me and not consistant with good design principals. What are you using these static variables for? If it is just String messages or words on a page, I'd follow the advice of Varun and look into f:loadBundle to load String resources from a properties file.



Agreed. If you are just seeking properties then use the included bundle functionality. If you are accessing data from a database like I did, you will have to try something different. All of my database calls were static methods belonging to a DAO class - I could not populate a dropdown combo box in JSF directly from this class. However, I made an instance variable in my backing bean that had the same type as the data being returned and in an accessor I called the static method from the DAO class.

I guess technically, my DAO class didn't have to be static to make this work. Then I could have populated the dropdown directly from the DAO. But I had my reasons
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Priti Shah:
Thanks for the reply.

I will then provide get methods for my static variables.


That shouldn't work. That's not what I meant.
 
Priti Shah
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think I am not understanding what you meant and I think I gave wrong example in my first post regarding how and why I want to use these static variables in my JSF page. Let me first clear few things.

1 )Those static string variables are actually used as a Keys in my HashMap and I am trying to access values related to that key of hashMap on JSF page.
(That is the reason I am not using message bundle for this strings)



2) I have one java class which declares all these static strings (keys)

3) Now I provided get methods in my managed bean to access these static variables.



So am I doing some thing wrong here? Please advise me if i am not understanding the concept clearly. I am new to JSF and still in learning phase.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!