【问题标题】:Struts2 localize interceptorStruts2 本地化拦截器
【发布时间】:2013-10-30 15:22:16
【问题描述】:

我正在尝试创建一个能够更改语言的拦截器,但我无法更改它。

相关拦截器代码:

public String intercept(ActionInvocation invocation) throws Exception {
    ActionMapping mapping = (ActionMapping) invocation
            .getInvocationContext()
            .get(ServletActionContext.ACTION_MAPPING);
     Map<String, Object> params = mapping.getParams();
     if (params != null) {
        Locale locale = (Locale) params.remove(LOCALE_PARAMETER);

        if (locale != null) {
             ActionContext.getContext().setLocale(locale);
        }
     }
     return invocation.invoke();
 }

struts.xml:

<package name="default" extends="struts-default" namespace="/">
    <result-types>
         <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
       </result-types>
     <interceptors>
         <interceptor name="navigator"
            class="it.apps.mca.web.interceptors.NavigatorInterceptor">
        </interceptor>
         <interceptor name="locale"
            class="it.apps.mca.web.interceptors.internationalizations.LocaleInterceptor">
        </interceptor>
        <interceptor-stack name="customStack">
            <interceptor-ref name="navigator" />
            <interceptor-ref name="locale" />
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
    </interceptors>
    <action name="locale"
        class="it.apps.mca.web.actions.internationalizations.LocaleAction">
        <interceptor-ref name="customStack"></interceptor-ref>
        <result name="success" type="redirect">
            <param name="location">${target}</param>
            <param name="parse">true</param>
        </result>
    </action>
    <action name="login"
        class="it.apps.mca.web.actions.authentication.LoginAction">
        <result name="success" type="tiles">/welcome.tiles</result>
        <result name="input">index.jsp</result>
        <result name="error">index.jsp</result>
    </action>
</package>

我知道 i18n 拦截器已经存在。 我添加了一个执行重定向的拦截器。添加此拦截器后,我丢失了页面的位置。 如果我通过操作执行重定向,也会发生同样的事情。你知道告诉我原因吗?

【问题讨论】:

    标签: java struts2 internationalization interceptor


    【解决方案1】:

    那个拦截器已经存在:i18n Interceptor

    它是 defaultStack 的一部分,因此您不必手动包含它。

    Example of usage

    this answer中的其他信息。

    【讨论】:

    • 我知道它已经存在。我添加了一个执行重定向的拦截器。添加此拦截器后,我丢失了页面的位置。如果我通过操作执行重定向,也会发生同样的事情。你知道告诉我原因吗?
    • 这不在您的拦截器代码中(可能是因为它不相关);顺便说一句,为什么你需要一个拦截器来做一些已经存在的事情?如果现有的已经在没有重定向的情况下处理它,为什么还需要重定向?
    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    相关资源
    最近更新 更多