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> ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.