ChangeLocaleActionpublic final class ChangeLocaleAction extends org.apache.struts.action.Action Implementation of Action that changes the user's |
(Omit source code)
Fields Summary |
---|
private Log | logCommons Logging instance. |
Methods Summary |
---|
public org.apache.struts.action.ActionForward | execute(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 properties on the given form, constructs an
appropriate Locale object, and sets it as the Struts Locale for this
user's session.
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".
// 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);
}
if ( (language != null && language.length() > 0) &&
(country != null && country.length() > 0) &&
(variant != null && variant.length() > 0) ) {
locale = new java.util.Locale(language, country, variant);
} else if ( (language != null && language.length() > 0) &&
(country != null && country.length() > 0)) {
locale = new java.util.Locale(language, country);
} else if (language != null && language.length() > 0) {
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);
|
|