• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

The locale changes after refrresh JSF

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to set the locale for my website and it works only until the next refresh of the page or until I navigate to the next pages. I read a lot about this subject but I cannot figure out what is the problem. Probably I missed something but I don't know what. Here is my code.

Locale Bean:

The xhml template for locale:

I tried to surround my template whith this tag, but after I had done this, the links of the template didn't work anymore.

   
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that "eager" does anything in Session scope. I've never used it, and if I did, it would have probably been in application scope, where it makes more sense.

I can't tell specifically what's wrong, but I do want to caution you about a very common misconception about JSF and listeners.

A JSF valuechangedlistener does not fire literally when a value is changed in the UI. In fact, it may not fire at all.

For a valuechangelistener to fire, you must first submit the form, either via a commandButton/Link or via AJAX. That sends the form fields (or in the case of AJAX, selected form fields) to the server where the JSF servlet processes them.

As part of the JSF lifecycle, the first thing that happens when a form is submitted is that all of the submitted values are validated. If even ONE value fails validation, the entire submit is rejected. Thus, the valuechangelistener does not fire.

On the other hand, if the data is valid, then the submitted value of the field attached to the listened is compared to the current backing bean propery value. If they are the same value, the listener does not fire and processing proceeds. If they are different, the listener fires.

The next step in the lifecycle is where JSF updates the backing bean property values from the validated data coming from the form submit, then the action method (for commands) or AJAX listener (for AJAX) will be invoked, secure in the knowledge that the backing bean data reflects what the user just entered and that it's all valid according to the validation constraints that were set in the View definition.

There aren't a whole lot of things that it is safe to do in a JSF listener. Most of the serious work should be done in an action or in an AJAX listener. So if I wanted to have a page where I pulled a locale selection from a drop-down control and the page immediately magically converted itself to Romanian, I'd define that dropdown with an AJAX sub-element that submitted only that control, put the locale-changing logic in an AJAX listener, and told the AJAX sub-element to refresh the entire page.

If you have properly saved the locale in a session-scoped object, JSF doesn't make unsolicited changes to that object, so navigation to a new page should retain that locale. That assumes that the session-scope object wasn't somehow destroyed between navigations (for example, via the session.invalidate() method) or was not in a multi-page session scope (in other words, it doesn't work in View Scope).

I have greatly simplified the JSF lifecycle here to emphasize the parts that are most important to you, in case you were wondering where some of the other functions of JSF processing were.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!