Java、Spring 将主题属性文件 Hook 到 css 文件

标签 java spring themes

这就是我初始化主题 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>

这是我的文件夹结构:

enter image description here

这些是我的 3 个属性文件的内部:

css=themes/black.css
css=themes/blue.css
css=themes/default.css

我也尝试过这些:

  1. css=classpath:themes/default.css
  2. css=/themes/default.css
  3. css=./themes/default.css

CSS 是否应该位于 WEB-INF/classes 中?我尝试过将其移出和移入等,但似乎从未完全使其正常工作。

以下是我将其放入 JSP 中的方式:

<link rel="stylesheet" href="<spring:theme code='css'/>" type="text/css" />

现在我的输出如下所示:

enter image description here

这是 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/

相关文章:

java - Java Set唯一对象标识

php - 需要修改 Wordpress 主页模板以包含编辑器字段的忽略内容

java - OJB 引用描述符 1 :0 relationship? 我是否应该将自动检索设置为 false?

mysql - JPA和MySQL : Knowing if start and end dates are aligned in a group by clause

cocoa - XCode 8.2 的深水色外观/自定义外观

visual-studio-code - 使用dark_vs.json的VSCode主题的WebStorm主题副本

java - Vaadin:带有右上角标题的窗口

java - 未正确添加到数组。希望得到帮助

Java:Linux 服务器上的数字签名与 Windows 客户端上的数字签名不同

java - 没有 Spring Boot 的 Java Spring MVC 中的 Auth0