Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the JSF equivalent of this JSP code?  RSS feed

 
James Greenberg
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I badly need to find a way to dynamically pass/change a value to my managed bean before using it, in JSP one can do it as easily as like this:

But there seems to be no way to do that in JSF, I hope I'm wrong.
I can't pass a default value to ${sLocaleLang} in the faces-config.xml file because the value of the "sLocaleLang" is calculated on the fly based on user's cookies values. I hope I made myself clear and really hoping for some good news..
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howzit James,

I've never used "usebean" before, so I can't really help you.

But, if you explain to me what the above code is busy doing, perhaps then I can help you with a jsf way of doing it.
 
James Greenberg
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My page has to display the list of several countries (about 30), their name must be in the language that the user prefers to see the content of the page, in my case these can be Russian, Romanian or English.
The cookie remembers the language of choice of the user, so the page reads the cookie and figures out the desired language and serves the country names in that language. All the country names are in a MySQL database, the table is called "countries", it has the columns "ru", "ro" and "en" (and and "id" ) which contain the corresponding translation of the countries names. Thus the bean must receive a hint as to which (language) fields to read and display to the user, and I can only know it at "run-time" (as this is user-specific).

I could store the names of the countries as a resource bundle (+ internationalization/localization technique), but I need to do different operations (add/select) on these lists so they must be/stay in a database..
 
Andres Quinones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,

What if in the Managed Bean you get the Locale from the FacesContext and depending on that you make the query to the Database.
 
James Greenberg
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that people here talk different langs and the "locale" is not reliable info (in this country, for different reasons), so our sites always have an option that says like "click here to view in such and such language". The page actually does read the locale, but only the first time to user accesses the site to try to pick the right language. So relying only on the locale unfortunately won't work fine..
 
James Greenberg
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is somewhat solved. I removed the JSF Bean (from faces-config.xml) and I'm using the JSP useBean technique to access the bean and set it's language parameter, then I'm using the id I provided in useBean inside my JSF tags and it seems to work. Time will tell whether this is reliable.

 
Andres Quinones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sLocaleLang value how is defined? You said from the cookie but how is that achieved? Because I was imaging to create on the header of the page a selectOneMenu with a list for the languages and store the selected value in the session in order to use it in any JSF Bean.

Or even better if you can access the parameter within ${} you can find it in the Request within the FacesContext right??? I think you can do it to use it in any JSF Bean. Because at the end all the information that comes from the cookie can be obtained within the FacesContext.

Hope this helps.
[ July 09, 2008: Message edited by: Andres Quinones ]
 
James Greenberg
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, in my first post the first snippet was not accurate, I have to use <%=sLocaleLang%>, ${sLocaleLang} doesn't seem to work since it's a regular String var that I create like this:

Thank you for your post/idea(s) I will consider them as well.
At the end of the day I wish there was the (exact)JSF equivalent for the JSP code, but who knows, perhaps there's a good reason for it not being there or it's there but I don't know about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!