开发者

Refreshing a page to load a new locale

I'm trying to make a JSF reload the locale (which is defined in the view)

<f:view locale="#{admin.userLocale}">
    <h:commandButton image="images/ukflag.gif" action="#{admin.returnToEnglish}" immediate="true" onclick="window.location.reload()" /> 
    <h:commandButton value="1" action="#{admin.returnToEnglish}" immediate="true" onclick="window.location.reload()" />
    <h:commandButton value="2" action="#{admin.returnToEnglish}" immediate="true" onclick="history.go(0)" /> 
    <h:commandButton value="3" action="#{admin.returnToEnglish}" immediate="true" onclick="window.location.href=window.location.href" /> 
    <h:commandLink value="4">
        <a4j:support event="onclick" reRender="GeneralConfigForm" action="#{admin.returnToEnglish}" />
    </h:commandLink>开发者_如何学Go;
</f:view>

I tried all of the above buttons. They all do the same thing - They call the method the changes the locale, the page reloads but the new locale labels are not loaded correctly. At this point, if I reload again the changes catch up and the locale is shown correctly.

How do I make the buttons update the local earlier so the page will show the new locale when it is brought up?

Thanks!

P.S - On JSF 1.2


I'm not sure how you got it to work after a refresh. Is the #{admin.userLocale} stored in the request scope instead of the session scope? The first four buttons won't invoke any JSF action at all. The JS function in the onclick get executed first and it changes the current page and hereby the default action is totally aborted. The fifth seem to be the only which should work, although a <h:form> is missing for all those buttons, but I'll bet that it's just the oversimplification of the code example.

Regardless, the usual practice is to store the user-specified locale in the session scope. When you want to change it by a JSF action method, then you need to send a redirect afterwards so that a new view will be created with the desired locale. Otherwise the initial locale will indeed be used in the response and thus you have to change it in the current view yourself by

FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);

in the action method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜