2016-04-16 42 views
0

我试图对URL做RequestMappingResources文件,根据当前Locale从资源和PropertyPlaceholderConfigurer的Spring RequestMapping?

我试图用PlaceHolders是可变的,但我知道它应该从Properties文件加载。除了我已经在运行时间因此将其加载为Bean它会一次只加载与默认Locale因此,即使改变了语言环境,它会不断加载从默认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 

控制器:

@RequestMapping(value = "/${url.controller1}", method = RequestMethod.GET) 
public String dd(ModelMap model){ 
    return "__front_container"; 
} 
+0

您想在请求期间更改区域设置吗?这是不可能的,因为占位符在创建applicationContext的过程中被替换,所以要用不同的值刷新它,你需要刷新一切。 – dambros

+0

@dambros我想根据当前语言环境从正确的资源中加载URL $ {url.controller1}',所以如果语言环境是'en'并且'url.controller1 = aa',那么'requestmap '自动成为/ aa,并且如果区域设置为'ab'且'url.controller1 = test','requestmap'为'/ test'等等。 – Jason4Ever

+0

在应用程序启动过程中,通过配置文件轻松实现,但在运行时无法更改。这可以吗?如果是这样,我可以告诉你一些例子 – dambros

回答

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(不带弹簧的Webflow),在这里看到了一个辉煌的解决方案:Spring MVC LocaleChangeInterceptor annotation based doesn't work

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