FileDocCategorySizeDatePackage
ChangeLocaleAction.javaAPI DocExample4157Wed Nov 03 13:09:14 GMT 2004com.oreilly.strutsckbk.ch12

ChangeLocaleAction

public final class ChangeLocaleAction extends org.apache.struts.action.Action
Implementation of Action 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, & page). Also changes the JSTL locale.

Fields Summary
private static final String
SUCCESS
private Log
log
Commons Logging instance.
Constructors Summary
Methods Summary
public org.apache.struts.action.ActionForwardexecute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

Change the user's @link(java.util.Locale) based on @link(ActionForm) properties.

This Action looks for language and country and variant properties on the given form, constructs an appropriate Locale object, and sets it as the Struts Locale for this user's session as well as the JSTL locale. Any ActionForm, including a @link(DynaActionForm), may be used.

If a page property is also provided, then after setting the Locale, control is forwarded to that URI path. Otherwise, control is forwarded to "success".

param
mapping The ActionMapping used to select this instance
param
form The optional ActionForm bean for this request (if any)
param
request The HTTP request we are processing
param
response The HTTP response we are creating
return
Action to forward to
exception
java.lang.Exception if an input/output error or servlet exception occurs


                                                                                                                                               
       
                  
                  
                  
      

        // Extract attributes we will need
        HttpSession session = request.getSession();
        Locale locale = getLocale(request);
    
        String language = null;
        String country = null;
        String variant = null;
        String page = null;
    
        try {
            language = (String) PropertyUtils.getSimpleProperty(form, "language");
            country = (String) PropertyUtils.getSimpleProperty(form, "country");
            variant = (String) PropertyUtils.getSimpleProperty(form, "variant");
            page = (String) PropertyUtils.getSimpleProperty(form, "page");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        boolean isLanguage = language != null && language.length() > 0;
        boolean isCountry  = country != null && country.length() > 0;
        boolean isVariant  = variant != null && variant.length() > 0;
        
        if ( isLanguage && isCountry && isVariant ) {
           locale = new java.util.Locale(language, country, variant);
        } else if ( isLanguage && isCountry ) {
            locale = new java.util.Locale(language, country);
        } else if ( isLanguage ) {
            locale = new java.util.Locale(language, "");
        }
    
        // reset the Struts locale
        session.setAttribute(Globals.LOCALE_KEY, locale);
            
        // reset the JSTL locale
        Config.set(session, Config.FMT_LOCALE, locale);
            
        if (null==page || "".equals(page))
            return mapping.findForward(SUCCESS);
        else
            return new ActionForward(page);