Question

I need some help with the following problem. We have an existing web application that uses no framework at all (just servlets) and we need to add new functionalities but instead of adding a new servlet to the web.xml, we are going to add Struts 1 along with Spring.

Since the project already exists, we have to configure Struts to be able to get along with the conventions already in place. One of those is localization.

Instead of using .property files, all localizable texts come from our database so I implemented my own MessageResourcesFactory and MessageResources classes. The problem is that in our application we get the user language from an Integer session attribute called "LANGUAGE" and not from the Locale attribute "org.apache.struts.action.LOCALE" that Struts uses by default.

The question is this. How do I tell Struts to fetch the user language from my custom Integer attribute instead of the stantard Locale attribute?

Thanks.

Was it helpful?

Solution

As far as I know there is no direct way to tell Struts to use another session attribute for using the locale. And even if Struts could do this, you'd need to implement a mapping between your custom integer and a "real" Locale object.

My suggestions:

  • Implement your own javax.servlet.http.HttpSessionAttributeListener which syncs the LANGUAGE attribute to the corresponding Struts locale attribute
  • Or change the code where you set your LANGUAGE attribute to also set the Struts session attribute

OTHER TIPS

Another solution would be setup a filter that will intercept the LANGUAGE and set the according Struts Locale before executiong the action:

public class localeFilter implements Filter  
{  

    public void destroy()  
    {  

    }  

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws Exception  
    {  
        // read the int LANGUAGE and set the Locale used by Struts
    }  

    public void init(FilterConfig filterConfig) throws ServletException  
    {  

    }  

}

And declare the filter in web.xml:

  <filter>  
    <filter-name>localeFilter</filter-name>  
    <filter-class>com.example.filters.MyFilter</filter-class>  
  </filter> 

  <filter-mapping>  
    <filter-name>localeFilter</filter-name>  
    <url-pattern>*.do</url-pattern>  
  </filter-mapping>  

The solution is to extend the ActionServlet and override methods doGet and doPost to change the locale via putting it into request before the process(). Like

String locale = "your_locale";
request.getSession().setAttribute(Globals.LOCALE_KEY, new Locale(locale));  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top