资源和PropertyPlaceholderConfigurer的Spring RequestMapping?

时间:2016-04-16 17:09:31

标签: java spring spring-mvc property-placeholder

我试图根据当前RequestMapping

Resources文件中的Locale做一个变量

我尝试使用PlaceHolders,但我知道它应该从Properties文件加载。除了我必须在运行时将其加载为Bean因此它将仅使用默认的Locale加载一次,因此即使更改了区域设置,它也将继续加载默认值{{ 1}}> Locale

任何想法?

我的尝试:

en_US

并在public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { setProperties(convertResourceBundleToProperties(ResourceBundle.getBundle("urls", LocaleContextHolder.getLocale()))); super.postProcessBeanFactory(beanFactory); } }

中致电
Bean

资源@Bean public CustomPropertyPlaceholderConfigurer CustomPropertyPlaceholderConfigurer(){ return new CustomPropertyPlaceholderConfigurer(); }

urls_ab.properties

控制器:

url.controller1=test

1 个答案:

答案 0 :(得分:0)

当您对支持PropertyPlaceholderConfigurer的属性文件进行更改时,您需要“刷新”应用程序以使更改生效。如果使用ConfigurableApplicationContext作为上下文,则可以在上下文中调用refresh。挑战在于,在Web应用程序中,您将依赖于web.xml而不是直接依赖于上下文对象,因此刷新以加载新的/更新的属性将需要重新启动应用程序...或者经历许多不必要的环节。请考虑以下是Spring Webflow应用程序中的示例。通过使用拦截器更新语言环境。 :

public class MyLocaleChangeInterceptor extends org.springframework.web.servlet.i18n.LocaleChangeInterceptor {
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
    if (locale != null) {
        try {
            response.setLocale(locale);
        } catch (Exception ex) {
            response.setLocale(Locale.ENGLISH);
        }
    } else {
        response.setLocale(Locale.ENGLISH);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    super.postHandle(request, response, handler, modelAndView);
}

}

 /** https://gist.github.com/jkuipers/3537965 Spring LocaleResolver that uses cookies but falls back to the HTTP Session when cookies are disabled*/
public class MyCookieLocaleResolver extends CookieLocaleResolver {
private SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
@Override
protected Locale determineDefaultLocale(HttpServletRequest request) {
    return sessionLocaleResolver.resolveLocale(request);
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    if (locale != null) {
        try {
            response.setLocale(locale);
        } catch (Exception ex) {
            response.setLocale(Locale.ENGLISH);
        }
    } else {
        response.setLocale(Locale.ENGLISH);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    super.setLocale(request, response, locale);
    sessionLocaleResolver.setLocale(request, response, locale);
}
@Override
public void setDefaultLocale(Locale defaultLocale) {
    sessionLocaleResolver.setDefaultLocale(defaultLocale);
}

}

<!--Then the XML:-->
<bean id="localeChangeInterceptor" class="MyLocaleChangeInterceptor">
    <property name="paramName" value="lang"/>
</bean>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="MyCookieLocaleResolver" >
    <property name="defaultLocale" value="en" />
</bean>


<!--Then Spring-webflow specific XML settings:-->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="order" value="2"/>
    <property name="flowRegistry" ref="flowRegistry" />
    <property name="interceptors" >
        <list>
            <ref local="localeChangeInterceptor" />
        </list>
    </property>
    <property name="defaultHandler">
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </property>
</bean>

如果使用Spring MVC(没有spring webflow),请参阅此处获得一个出色的解决方案:Spring MVC LocaleChangeInterceptor annotation based doesn't work

MyKong也提供了一个很好的解决方案:http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/