Themes
你可以应用 Spring Web MVC 框架主题来设置你的应用程序的总体外观和感受,从而增强用户体验。主题是静态资源(通常是样式表和图像)的集合,它影响应用程序的视觉风格。
You can apply Spring Web MVC framework themes to set the overall look-and-feel of your application, thereby enhancing user experience. A theme is a collection of static resources, typically style sheets and images, that affect the visual style of the application.
从 6.0 开始,不再支持主题,而是建议使用 CSS,且在服务器端没有任何特殊支持。
as of 6.0 support for themes has been deprecated theme in favor of using CSS, and without any special support on the server side.
Defining a theme
要在 Web 应用程序中使用主题,你必须设置 org.springframework.ui.context.ThemeSource
接口的实现。WebApplicationContext
接口扩展了 ThemeSource
,但将其职责委托给专用的实现。默认情况下,委托是一个 org.springframework.ui.context.support.ResourceBundleThemeSource
实现,从类路径的根加载属性文件。要使用自定义 ThemeSource
实现或配置 ResourceBundleThemeSource
的基本名称前缀,你可以在应用程序上下文中注册一个 bean,其保留名称为 themeSource
。Web 应用程序上下文会自动检测具有该名称的 bean 并使用它。
To use themes in your web application, you must set up an implementation of the
org.springframework.ui.context.ThemeSource
interface. The WebApplicationContext
interface extends ThemeSource
but delegates its responsibilities to a dedicated
implementation. By default, the delegate is an
org.springframework.ui.context.support.ResourceBundleThemeSource
implementation that
loads properties files from the root of the classpath. To use a custom ThemeSource
implementation or to configure the base name prefix of the ResourceBundleThemeSource
,
you can register a bean in the application context with the reserved name, themeSource
.
The web application context automatically detects a bean with that name and uses it.
当你使用 ResourceBundleThemeSource
时,主题是在一个简单的属性文件内定义。该属性文件会列出组成主题的资源,如下所示:
When you use the ResourceBundleThemeSource
, a theme is defined in a simple properties
file. The properties file lists the resources that make up the theme, as the following example shows:
styleSheet=/themes/cool/style.css background=/themes/cool/img/coolBg.jpg
属性的键是从视图代码中引用主题元素的名称。对于 JSP,通常使用 spring:theme
自定义标签,其与 spring:message
标签非常相似。以下 JSP 片段使用在前一个示例中定义的主题来自定义外观:
The keys of the properties are the names that refer to the themed elements from view
code. For a JSP, you typically do this using the spring:theme
custom tag, which is
very similar to the spring:message
tag. The following JSP fragment uses the theme
defined in the previous example to customize the look and feel:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
</head>
<body style="background=<spring:theme code='background'/>">
...
</body>
</html>
默认情况下,ResourceBundleThemeSource
使用一个空基本名称前缀。因此,属性文件从类路径的根加载。这样的话,你可以将 cool.properties
主题定义放置在类路径根目录下的一个目录(例如 /WEB-INF/classes
)中。ResourceBundleThemeSource
使用标准 Java 资源包加载机制,允许主题进行完全国际化。例如,我们可以有一个 WEB-INF/classes/cool_nl.properties
,它引用包含荷兰语文本的特殊背景图像。
By default, the ResourceBundleThemeSource
uses an empty base name prefix. As a result,
the properties files are loaded from the root of the classpath. Thus, you would put the
cool.properties
theme definition in a directory at the root of the classpath (for
example, in /WEB-INF/classes
). The ResourceBundleThemeSource
uses the standard Java
resource bundle loading mechanism, allowing for full internationalization of themes. For
example, we could have a /WEB-INF/classes/cool_nl.properties
that references a special
background image with Dutch text on it.
Resolving Themes
按照 preceding section 中所述定义主题后,需要决定使用哪个主题。DispatcherServlet
查找名为 themeResolver
的 bean 以找出要使用哪个 ThemeResolver
实现。主题解析器的作用与 LocaleResolver
非常相似。它检测要用于特定请求的主题,还可以更改请求的主题。下表描述了 Spring 提供的主题解析器:
After you define themes, as described in the preceding section,
you decide which theme to use. The DispatcherServlet
looks for a bean named themeResolver
to find out which ThemeResolver
implementation to use. A theme resolver works in much the same
way as a LocaleResolver
. It detects the theme to use for a particular request and can also
alter the request’s theme. The following table describes the theme resolvers provided by Spring:
Class | Description |
---|---|
|
Selects a fixed theme, set by using the |
|
The theme is maintained in the user’s HTTP session. It needs to be set only once for each session but is not persisted between sessions. |
|
The selected theme is stored in a cookie on the client. |
Spring 还提供一个 ThemeChangeInterceptor
,它允许在每个请求中使用一个简单的请求参数来更改主题。
Spring also provides a ThemeChangeInterceptor
that lets theme changes on every
request with a simple request parameter.