Developer's experience

July 20, 2009

Using Locale in Struts1/JSTL application

Filed under: Uncategorized — migut @ 11:10

When u are creating multi-language Struts and JSTL web application, then it is important to change also locale for FMT config… then u can use fmt:message tag to display property:
<fmt:message key=”app.link.locale.text”/>

sample action class below. Enjoy!

import java.util.Locale;

import com.plansolutions.web.action.BaseActionWithSpringIntegration;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.core.Config;

public class LocaleAction extends BaseActionWithSpringIntegration {
   
    private static String LOCALE_PARAM=”locale”;
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug(“Entering ‘LocaleAction’ method…”);
        }        
        if (request.getParameter(LOCALE_PARAM)!=null) {            
            if (request.getParameter(LOCALE_PARAM).indexOf(“en”)>=0)
                setLocale(request, Locale.ENGLISH);
            else
                setLocale(request, Locale.GERMAN);
        }
        if (log.isDebugEnabled())
            log.debug(“setLocale after=”+request.getSession().getAttribute(“org.apache.struts.action.LOCALE”));
        return mapping.findForward(“referer”);
    }
    
    @Override
    protected void setLocale(HttpServletRequest request, Locale locale) {        
        super.setLocale(request, locale);
        Config.set(request.getSession(), Config.FMT_LOCALE, locale);
        if (log.isDebugEnabled())
            log.debug(“setLocale=”+locale);
    }
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: