6 Replies Latest reply on Apr 7, 2009 6:46 AM by nbelaevski

    a4j and jsf locale change problem



      I'm very new to both jsf and a4j programming and I'm currently stuck and out of ideas on how to solve the problem mentioned in the topic.

      I'm using a4j to dynamically validate some inputfields on a form in following manner:

      <h:inputText id="firstNameInput" value="#{userBean.firstName}" size="40" maxlength="50" required="true">
      <f:validateLength minimum="5" maximum="50"/>
      <f:validator validatorId="nameValidator"/>
      <a4j:support event="onblur"
      <h:outputText id="errorMessage" binding="#{userBean.error}"
      style="color: red; font-style: italic;"/>

      and I also allow locale change with the following buttons:
      <h:commandLink action="#{changeLocaleBean.englishAction}" immediate="true">
      <h:graphicImage value="/images/english_flag.gif" style="border: 0px"/>


      public String germanAction() {
      FacesContext context = FacesContext.getCurrentInstance();
      return null;

      The locale change works perfectly well on all non-ajax components of the website however since the ajax component - errorMessage, obtains its value from the userBean and not directly from the message_*.properties files and the command button does not invoke a new validation the message stays in the previous locale.

      Is there any way to invoke verification, which would in due course rerender the proper errorMessage stratight after the locale change?

      Or is there perhaps some different way to manage the locale change or errorMessage display which would simplyfy or solve this problem?

      Thank you in advance for your reply.

        • 1. Re: a4j and jsf locale change problem


          You can make h:inputText immediate="true". This will cause validation to trigger for immediate action components also.

          • 2. Re: a4j and jsf locale change problem


            Thanks for the idea - it does not however solve the problem.

            The thing is that the ajax component which is triggered by an onblur event retrieves the error message from a .properties file and it does not automatically react to locale change. The message gets properly adjusted to the correct locale after I trigger the onblur event myself manually.

            Maybe there is some way to trigger this event from the commandLinks straight after the locale change?


            • 3. Re: a4j and jsf locale change problem


              Please post bean code. And what is bean scope?

              • 4. Re: a4j and jsf locale change problem

                the userBean ajax validation code :

                private String errorMessage = null;

                public String getErrorMessage() {
                return errorMessage;

                public void setErrorMessage(String errorMessage) {
                this.errorMessage = errorMessage;

                public void validateName(ActionEvent e) {

                UIInput input = (UIInput)e.getComponent() // Ajax4jsf comp
                .getParent(); // input comp
                if (input != null) {
                String name = (String)input.getSubmittedValue();
                if (name != null) {
                //do something

                FacesContext fc = FacesContext.getCurrentInstance();
                input.validate(fc); // iterates over input's validators
                if ( ! input.isValid())
                setErrorMessage(fc, input);
                private void setErrorMessage(FacesContext fc, UIInput input) {

                Iterator it = fc.getMessages(input.getClientId(fc));
                if (it.hasNext()) {
                FacesMessage facesMessage = (FacesMessage)it.next();
                errorMessage = facesMessage.getSummary();

                This code is triggered by an onblur event on the firstNameInput field and the errorMessage is retrieved from the errorMessage property.

                Now when the commandLink changes the locale the JSF changes all of the labels which directly access their strings via eg. #{msgs.firstName}, however since the errorMessage is taken from the bean property - it does not get changed automatically


                • 5. Re: a4j and jsf locale change problem

                  forgot to add that both the userBean and changelocaleBean are session scoped.

                  • 6. Re: a4j and jsf locale change problem

                    I think the best you can do are two things: either a) call validating actions programmatically on locale switch
                    or b) use more common way of validation