When I used CookieLocaleResolver, I can set invali

2019-07-21 11:12发布

In web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <display-name>Spring MVC Servlet</display-name>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

In spring-dispatcher-servlet.xml

<context:component-scan base-package="lei.spring.mvc.controller" />

In applicationContext.xml

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" p:cookieName="clientLang" />

After I set the content of clientLang cookie to en-CA, spring only returns the following exception to my browser:

java.lang.IllegalArgumentException: Locale part "en-CA" contains invalid characters
at org.springframework.util.StringUtils.validateLocalePart(StringUtils.java:710)
at org.springframework.util.StringUtils.parseLocaleString(StringUtils.java:690)
at org.springframework.web.servlet.i18n.CookieLocaleResolver.parseLocaleCookieIfNecessary(CookieLocaleResolver.java:164)
at org.springframework.web.servlet.i18n.CookieLocaleResolver.resolveLocaleContext(CookieLocaleResolver.java:136)
at org.springframework.web.servlet.DispatcherServlet.buildLocaleContext(DispatcherServlet.java:1043)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:955)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)

Then I try to create my own MyCookieLocaleResolver class to catch all exception and return default Locale instance:

public class MyCookieLocaleResolver extends CookieLocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
    try {
        return super.resolveLocale(request);
    } catch (Exception exception) {
        return new Locale.Builder().setLanguage("en").setRegion("CA").build();
    }
}
}

changed applicationContext.xml into :

<bean id="localeResolver" class="package.MyCookieLocaleResolver" p:cookieName="clientLang" />

But spring still returns the same exception to my browser. I read the code of DispatcherServlet. Seems the instance this.localeResolver is null, which is not from localeResolver instance. It always use CookieLocaleResolver instance. How can I set localeResolver instance into DispatcherServlet?

protected LocaleContext buildLocaleContext(final HttpServletRequest request) {

    if (this.localeResolver instanceof LocaleContextResolver) {
        return ((LocaleContextResolver) this.localeResolver).resolveLocaleContext(request);
    }
    else {
        return new LocaleContext() {
            @Override
            public Locale getLocale() {
                return localeResolver.resolveLocale(request);
            }
        };
    }
}

3条回答
祖国的老花朵
2楼-- · 2019-07-21 11:52

Since version 4.3 there is a new parameter languageTagCompliant which enables languageTag parsing

CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setLanguageTagCompliant(true);
查看更多
混吃等死
3楼-- · 2019-07-21 11:53

After I overrided two methods, it works fine:

public class MyCookieLocaleResolver extends CookieLocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
    try {
        return super.resolveLocale(request);
    } catch (Exception exception) {
        return Locale.forLanguageTag("en-CA");
    }
}

@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
    try {
        return super.resolveLocaleContext(request);
    } catch (Exception exception) {
        return new LocaleContext() {
            @Override
            public Locale getLocale() {
                return Locale.forLanguageTag("en-CA");
            }
        };
    }
}

}

查看更多
我命由我不由天
4楼-- · 2019-07-21 12:05

try using only "en" as default locale:

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en" />
    <property name="cookieName" value="clientLang" />
    <property name="cookieMaxAge" value="31556926" />
</bean>
查看更多
登录 后发表回答