STRUTS2 国际化

STRUTS2 国际化
1.过滤器
package  com.i18n;

import  java.io.IOException;

import  javax.servlet.Filter;
import  javax.servlet.FilterChain;
import  javax.servlet.FilterConfig;
import  javax.servlet.ServletException;
import  javax.servlet.ServletRequest;
import  javax.servlet.ServletResponse;
import  javax.servlet.http.HttpServletRequest;

public   class  I18nFilter  implements  Filter
{
    
    
public void destroy()
    
{}
    
    
public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain filterChain) 
throws IOException, ServletException
    
{
        HttpServletRequest request 
= (HttpServletRequest) req;
        MyRequestWrapper wrapper 
= new MyRequestWrapper(request);
        filterChain.doFilter(wrapper, resp);
    }

    
    
public void init(FilterConfig arg0) throws ServletException
    
{}
}



2.过滤器配置
< filter >    
        
< filter-name > i18nFilter </ filter-name >    
        
< filter-class > com.xxx.common.filter.I18nFilter </ filter-class >    
    
</ filter >    
    
< filter-mapping >    
        
< filter-name > i18nFilter </ filter-name >    
        
< url-pattern > * </ url-pattern >    
   
</ filter-mapping >   


3.包装类
package  com.i18n;

import  java.util.Locale;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletRequestWrapper;
import  javax.servlet.http.HttpSession;

public   class  MyRequestWrapper  extends  HttpServletRequestWrapper
{
    
    
private Locale locale = null;
    
    
public MyRequestWrapper(HttpServletRequest request)
    
{
        
super(request);
        HttpSession session 
= request.getSession();
        locale 
= (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
    }

    
    
public String getHeader(String name)
    
{
        String value 
= super.getHeader(name);
        
if ("Accept-Language".equals(name) && locale != null)
        
{
            value 
= locale.getLanguage() + "_" + locale.getCountry()
                    
+ value.substring(6, value.length());
        }

        
return value;
    }

    
    
public Locale getLocale()
    
{
        
if (locale != null)
        
{
            
return locale;
        }

        
return super.getLocale();
    }

    
}

你可能感兴趣的:(STRUTS2 国际化)