【问题标题】:How to Load lables via Properties file in FreeMarker如何通过 FreeMarker 中的属性文件加载标签
【发布时间】:2014-12-15 01:54:55
【问题描述】:

我正在使用 Spring MVC + FreeMarker 集成。由于我是 FreeMarker 的新手,我无法找到从属性文件中配置 FreeMarker 标签的方法。

请帮我解决这个问题。

谢谢。

【问题讨论】:

    标签: spring-mvc freemarker labels


    【解决方案1】:

    您可以为此使用“ResourceBundleMessageSource”消息源。

    <bean id="messageSource"   
               class="org.springframework.context.support.ResourceBundleMessageSource"
          <property name="basename" value="classpath:messages/messages" />
          <property name="defaultEncoding" value="UTF-8"/>
    />
    

    定义语言环境解析器,

    <bean id="localeResolver"
            class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
            <property name="defaultLocale" value="en" />
        </bean>
    

    定义从用户会话中检测语言参数并调用 locale resolve 的 localeChangeInterceptor。并在处理程序映射中注册拦截器。

    <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
    </bean>
    

    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
            <property name="interceptors">
               <list>
                <ref bean="localeChangeInterceptor" />
               </list>
            </property>
        </bean>
    

    需要像这样定义属性文件,

    • messages_en.properties
    • messages_ar.properties

    在消息文件夹路径中。

    如果语言属性文件频繁更改,则可以使用“ReloadableResourceBundleMessageSource”。这意味着您不必在每次更改语言文件时都重新启动应用程序。

    需要导入spring宏,

    <#import "/spring.ftl" as spring/>
    

    可以在 .ftl 中访问消息,如下所示。

    <@spring.message "customMessageKey"/> 
    

    【讨论】:

    • 它如何决定使用messages_en.properties 还是messages_ar.properties?
    • 检查更新的答案。 Interceptor 提供了 localeResolver 中使用的语言参数。
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多