spring mvc配置主題和國際化

在web.xml 中需要加上ContextLoaderListener的監聽器,否則在解析jsp的時候會報找不到ApplicationContext的錯誤。</span>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-servlet.xml</param-value>
  </context-param>
配置主題:把theme.properties 放到classpath根目錄下 其中配置logo=imgs/logo

在jsp中先加上spring 的標籤庫

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

然後用
<spring:theme code='logo' />
即可取出logo的值

如果自定義的話,需要如下配置:

 <!-- 默認主題-->
    <bean id="themeResolver" class="org.springframework.web.servlet.theme.FixedThemeResolver">
        <property name="defaultThemeName" value="theme2" />
    </bean>
    <!-- 國際化 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*"/>
            <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
                <property name="paramName" value="theme" />
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
但是注意, 不能用jsp直接訪問,因爲必須要過過濾器,而jsp不會過過濾器。

配置國際化

在springmvc 配置文件中加上以下配置

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>
即爲引用前綴爲messages的國際化配置文件 比如 messages_zh_CN.properties文件

然後在jsp中加上 

<spring:message code='logo' />
即可得到配置文件中logo的值

發佈了31 篇原創文章 · 獲贊 16 · 訪問量 8958
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章