这就是我初始化主题 bean 的方式:
<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="resources.theme-" />
</bean>
<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>
这是我的文件夹结构:
这些是我的 3 个属性文件的内部:
css=themes/black.css
css=themes/blue.css
css=themes/default.css
我也尝试过这些:
- css=classpath:themes/default.css
- css=/themes/default.css
- css=./themes/default.css
CSS 是否应该位于 WEB-INF/classes 中?我尝试过将其移出和移入等,但似乎从未完全使其正常工作。
以下是我将其放入 JSP 中的方式:
<link rel="stylesheet" href="<spring:theme code='css'/>" type="text/css" />
现在我的输出如下所示:
这是 JSP 的一部分:
<span style="float: left">
<a href="?theme=default">def</a>
|
<a href="?theme=black">blk</a>
|
<a href="?theme=blue">blu</a>
</span>
这是我的 black.css。
body {
background-color: #888;
color: white;
}
我的问题是,CSS 似乎永远不会出现在我的 JSP 文件中。这可能是因为属性文件找不到CSS文件造成的... 请随时询问更多信息。
最佳答案
我认为这有问题。
<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="resources.theme-" />
</bean>
根据 Javadoc:
public void setBasenamePrefix(String basenamePrefix)
Set the prefix that gets applied to the ResourceBundle basenames, i.e. the theme names. E.g.: basenamePrefix="test.", themeName="theme" -> basename="test.theme".
Note that ResourceBundle names are effectively classpath locations: As a consequence, the JDK's standard ResourceBundle treats dots as package separators. This means that "test.theme" is effectively equivalent to "test/theme", just like it is for programmatic java.util.ResourceBundle usage.
此外,您还可以将主题属性文件放入 WEB-INF/classes 文件夹中,如引用文档中所述。
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.
我猜你的情况的问题是你已经两次提到了css的路径..一次在<property name="basenamePrefix" value="resources.theme-" />
中也进入属性文件
css=themes/black.css
css=themes/blue.css
css=themes/default.css
关于Java、Spring 将主题属性文件 Hook 到 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7063191/