Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Facelets - setting locale via link

843844Mar 15 2010 — edited Mar 23 2010
Hello Everyone,

I am developing a small web app. A file upload with additional params, its processing on submit and returning the final chart (an image). There are two links for switching to another locale and one for a help page.

The first version was made as JSP page with EL tags and backing servlet. It worked fine but I found the final image can't be saved on local disk. Later I figured out it is caused by browser behaviour during 'save' action, when the servlet is called again (if the image is not already in cache), but as original parameters are missing in this phase, creating of the final image fails.

Trying to solve my problem I've found PRG pattern [1] which seems to be promising answer. I've chosen facelets framework (Mojarra) and tried to refactor app logic a bit. As I see, the complexity of the whole solution grows... and not everything I am able to solve...

My current blocker is a language switcher. What could be in JSP made relatively easy using the following code at the beginning:
<c:if test="${param['locale'] != null}">
  <fmt:setLocale value="${param['locale']}" scope="session" />
</c:if>
and handled by very simple links:
<a href="index.jsp?locale=en-GB">English</a> | <a href="index.jsp?locale=cs-CZ">Czech</a>
is now unsolvable issue for me.

After long trial error path I set up the following way, but it still doesn't work properly.

In xhtml:
<f:view encoding="utf-8" locale="#{localeBean.locale}">

<h:form>
  <h:commandLink action="#{localeBean.setLocaleEx}">
    <f:setPropertyActionListener
       target="#{localeBean.locale}" value="en"/>
    English
  </h:commandLink>

  <h:commandLink action="#{localeBean.setLocaleEx}">
    <f:setPropertyActionListener
       target="#{localeBean.locale}" value="cs"/>
    Czech
  </h:commandLink>

  <h:outputText value="#{localeBean.locale}" />
  ...
In Bean:
@ManagedBean
@RequestScoped
public class localeBean {

    protected String locale = "cs";

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public void setLocaleEx() {
    }
}
Click on generated link really change localeBean.locale value which is displayed using directive h:outputText, but...
1. refreshing of the page is very very slow on my quite recent PC (I use Tomcat 6 as the container - the JSP delay was undetectable)
2. no locale is changed (a f:view locale atribute is probably ignored)
3. in the final HTML page there is a quite long JavaScript

... so unacceptable for me.

Can I create links which can change locale more straigforward and reliable way? And without that JavaScript?

Can I set f:view locale using any parameter sent using a simple URL link like Czech ? I need this setting also for Help page, so a session scope is required. Can this be done via facelets?

Curently I am puzzled a bit...

Thanks in advance for any hint.

Regards,
Jan

____________
[1] [http://blogs.sun.com/enterprisetechtips/entry/post_redirect_get_and_jsf|http://blogs.sun.com/enterprisetechtips/entry/post_redirect_get_and_jsf]
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2010
Added on Mar 15 2010
2 comments
221 views