Spring3MVC:Themes in Spring-Tutorial with Example

我們的目的爲了改變Hello World Spring3MVC,添加對主題的支持,用戶將可以選擇主題從預定義的主題中,(default,black,blue)

添加主題支持,在spring3MVC中

添加下面的代碼到spring-servlet.xml中

File: WebContent/WEB-INF/spring-servlet.xml

<bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <property name="basenamePrefix" value="theme-" />
</bean>
 
<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <property name="paramName" value="theme" />
</bean>
<bean id="themeResolver"
    class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <property name="defaultThemeName" value="default" />
</bean>
 
 
<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor" />
            <ref bean="themeChangeInterceptor" />
        </list>
    </property>
</bean>
File: resources/theme-default.properties

css=themes/default.css

File: resources/theme-blue.properties

css=themes/blue.css

File: resources/theme-black.properties

css=themes/black.css

爲不同的主題設定不同的css

讓我們創建css文件在WebContent/themes目錄中

File: WebContent/themes/default.css

body {
    background-color: white;
    color: black;
}
File: WebContent/themes/blue.css

body {
    background-color: #DBF5FF;
    color: #007AAB;
}
File: WebContent/themes/black.css

body {
    background-color: #888;
    color: white;
}
File: WebContent/WEB-INF/jsp/header.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
 
<h3><spring:message code="label.title"/></h3>
 
<span style="float: right">
    <a href="?lang=en">en</a>
    |
    <a href="?lang=de">de</a>
</span>
 
 
<span style="float: left">
    <a href="?theme=default">def</a>
    |
    <a href="?theme=black">blk</a>
    |
    <a href="?theme=blue">blu</a>
</span>
程序運行ok!


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