1

After changing locale in action mathod with return null as I apologize page should be reload but field with date doesn't change after first click on link for changing locale. I use MyFaces

I have a page where user can set locale and after changing locale I can't get desired format of date just after second time push on link for changing locale. I use bean for localization from this post

localiztion article

I cant understand why in

<h:outputText value="#{newsVar.date}">
<f:convertDateTime pattern="#{msg['date.pattern']}" />
</h:outputText>

!!!! Edited!!! I have some problems with getting id of dataField whem I use facets in mainLayout.xhtml

<f:view locale="#{localeManager.language}">

            <div id="wrapper">
                <div class="header">
                    <ui:insert name="header">
                        <ui:include src="/pages/template/commonHeader.xhtml" />
                    </ui:insert>
                </div>
                <div class="content_container">
                    <ui:insert name="content" />
                </div>
            </div> 
</f:view>

commonHeader is included commandLink

    <ui:composition>
        <h:form>
            <h1>
                <h:outputText value="#{msg['header.text']}" />
            </h1>
            <div class="lang_selector">             
                <h:commandLink action="#{localeManager.changeLanguage('ru')}"
                    value="#{msg['header.language.ru']}">
<!--                    <f:ajax render=":listGroupId:dateField" />-->
                </h:commandLink>
                <h:commandLink action="#{localeManager.changeLanguage('en')}"
                    value="#{msg['header.language.en']}">
<!--                    <f:ajax render=":listGroupId:dateField" />-->
                </h:commandLink>
            </div>
        </h:form>
    </ui:composition>

And in current page I have next

<ui:composition template="/pages/layouts/mainLayout.xhtml">
        <ui:define name="content">
            <f:event type="preRenderView" listener="#{news.showNews}" />
            <h:form id="listForm" prependId="false">
                        <h:panelGroup id="listGroupId">             
                <ui:repeat var="newsVar" value="#{news.newsList}">
                    <div class="news_item">
                        <div class="news_title">
                            <h1>
                                <h:outputText value="#{newsVar.title}" />
                            </h1>
                            <div class="news_date">
                                <h:outputText id="dateField" value="#{newsVar.date}">
                                    <f:convertDateTime pattern="#{msg['date.pattern']}" />
                                </h:outputText>
                            </div>

As you can see a try to get id of "dateField" but that id in different form. I read next article How to JSF 2.0: – render components outside of the form I understand that I should set <h:form id="listForm" prependId="false"> then I can set elementId in <f:ajax/> without name of form but I get error javax.faces.FacesException - Component with id::listGroupId:dateField not found.

And I find that: The element which is specified in render must be already present in the client side HTML DOM tree. May be that's why I have this problem

Please, help me... Why is pattern for date changed only from 2nd time?

Community
  • 1
  • 1
Ray
  • 1,788
  • 7
  • 55
  • 92
  • 2
    do you re render your `h:outputText` after you change the pattern ? – Daniel Oct 15 '12 at 09:53
  • if I know rendered=true by default – Ray Oct 15 '12 at 09:56
  • 1
    I meant that after you "changing locale" you probably clicking on some button or other component , that button should have `f:ajax render="your_outputtext_idOryour_outputtext_wrapper_id"`... – Daniel Oct 15 '12 at 10:04
  • I need a variant without ajax. Any other ideas? – Ray Oct 15 '12 at 13:45
  • you can reload the whole page (BAD) , otherwise I don't know how else you can reload from server , you need to call the `f:convertDateTime` , and you can't do it with client side... so you better use `f:ajax` , that's what the JSF2 is for... – Daniel Oct 15 '12 at 13:47
  • I don't click after changing locale (just reload page) `` I have next link in header faclet of my page – Ray Oct 15 '12 at 14:13
  • @Daniel please, can you give me advice. I described appeared problem above – Ray Oct 15 '12 at 20:38
  • try to update a broader id , maybe some wrapper , do you use primeafes ? cause if yes, you can update by specifying css class name – Daniel Oct 16 '12 at 07:19
  • Myfaces is a JSF implementation , while Primefaces is JSF Component Suite , http://stackoverflow.com/questions/7316611/icesfaces-vs-myfaces-vs-primefaces read balusC comment too... anyway try to find an id that you can reach in the render attribute , or think of redesign ... – Daniel Oct 16 '12 at 07:30
  • @Daniel I think that problem not in re-rendering of the field. Problem with convertDateTime – Ray Oct 16 '12 at 07:35
  • if you think so, than try to create a super minimal example , and see if it works... – Daniel Oct 16 '12 at 07:50

1 Answers1

1

I would recommend the following

1) change setLanguage into changedLanguage (inside that method set the language attribute) in general , its a bad practive to use setters in action attribute , and in general use get/set prefix in actionmethods

2) add f:ajax

<h:commandLink action="#{localeManager.changedLanguage('en')}" value="#msg['header.language.en']}">
    <f:ajax render="dateField"/>
</h:commandLink>

3) change h:outputText into

<h:outputText id="dateField" value="#{newsVar.date}">
    <f:convertDateTime pattern="#{msg['date.pattern']}" />
</h:outputText>
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • May be it's a good solution but I can't do the same. I add some info in question above – Ray Oct 15 '12 at 16:29