Spring mvc ResourceBundleMessageSource用于不同的主题

时间:2011-11-21 06:42:55

标签: java spring-mvc themes

我配置我的appstrings.properties文件,其中包含与我的应用程序使用的主题相关的字符串。我的申请有两个主题。两者都有自己的appstrings.properties文件位于WEB-INF/strings/theme1/appstringsWEB-INF/strings/theme2/appstrings。我在config.properties文件中指定了属性themeName。

这是我的spring配置文件:

<context:property-placeholder location="file:///${config.properties}" />

...

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="WEB-INF/strings/theme1/appstrings" />

现在我的所有app字符串都来自WEB-INF / strings / theme1 / appstrings.properties文件。我怎样才能让它变得动态。即当我将themeName属性更改为theme2时,它应该从WEB-INF / strings / theme2 / appstrings.properties获取字符串

2 个答案:

答案 0 :(得分:1)

您需要使用ResourceBundleThemeSource

<!-- resolves localized <theme_name>.properties files in the classpath to 
    allow for theme support -->
<bean
    class="org.springframework.ui.context.support.ResourceBundleThemeSource"
    id="themeSource" /> 
 <mvc:interceptors>
    <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/> 
 </mvc:interceptors>

请参阅Spring Reference Chapter 15.7 Using Themes

对于正在运行的示例,您可以快速创建一个小型Spring Roo应用程序。它使用主题支持来更改css文件。

答案 1 :(得分:0)

如果config.properties包含一个属性,请说:themeName = propertyValue。如果要在spring mvc配置servlet xml文件中使用此themeName的值,则可以使用$ {themeName}

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="WEB-INF/strings/${themeName}/appstrings" />

reference