2013年1月15日 星期二

Integrate Bean Validation (JSR303) with Struts2 -- SpringI18nInterceptor

Integrate Bean Validation (JSR303) with Struts2 -- Outline
JSR303本身就有提供i18n的功能,但是因為我們要和Struts2整合,所以i18n的部分必須和Struts2同步,這支interceptor的功能就是用來取得Struts2目前的locale,然後傳給Spring,須注意這支interceptor的位置必須在Struts2 i18n interceptor之後。
com.gss.gmo.cao.struts2.interceptor.SpringI18nInterceptor
package com.gss.gmo.cao.struts2.interceptor;

import java.util.Locale;

import org.springframework.context.i18n.LocaleContextHolder;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * Populate
 * org.springframework.context.i18n.LocaleContextHolder.setLocale(Locale) by the
 * result from com.opensymphony.xwork2.interceptor.I18nInterceptor.
 * 
 * @author linus_chien
 * 
 */
public class SpringI18nInterceptor extends AbstractInterceptor {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        Locale locale = invocation.getInvocationContext().getLocale();
        LocaleContextHolder.setLocale(locale);
        String result = invocation.invoke();
        return result;
    }

}