O'Reilly logo

Jakarta Struts Cookbook by Bill Siggelkow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.4. Changing Locale on the Fly

Problem

You want to allow a user to choose the language and country to be used by the web application for his session.

Solution

Use my ChangeLocaleAction, based on the Struts built-in LocaleAction, as shown in Example 12-5.

Example 12-5. Struts action for changing the current locale

package com.oreilly.strutsckbk.ch12; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.jstl.core.Config; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * Implementation of <strong>Action</strong> that changes the user's * @link(java.util.Locale and forwards to a page, based on request level * parameters that are set (language, country, variant, &amp; page). * Also changes the JSTL locale. */ public final class ChangeLocaleAction extends Action { private static final String SUCCESS = "success"; /** * Commons Logging instance. */ private Log log = LogFactory.getFactory( ).getInstance(this.getClass( ). getName( )); /** * <p> * Change the user's @link(java.util.Locale) based on @link(ActionForm) * properties. * </p> * <p> * This <code>Action</code> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required