我們的目的爲了改變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
>
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
;
}
body
{
background-color
:
#DBF5FF
;
color
:
#007AAB
;
}
body
{
background-color
:
#888
;
color
:
white
;
}
<%@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
>