简体   繁体   English

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

[英]Java, Spring hooking themes properties file to css file

this is how I intialize theme beans:这就是我初始化主题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>

this is my folder stucture:这是我的文件夹结构:

在此处输入图像描述

these are the inside of my 3 properties files:这些是我的 3 个属性文件的内部:

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

I have also tried these:我也试过这些:

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

Should the css even be in WEB-INF/classes. css 是否应该在 WEB-INF/classes 中。 I've tried moving it out and in etc, but never quite seemed to get it to work.我已经尝试将它移出和移入等,但似乎从来没有让它工作。

Here is how I put it in my JSP:这是我将它放入我的 JSP 的方式:

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

This is now my output looks like:现在我的 output 看起来像:

在此处输入图像描述

This is part of JSP:这是 JSP 的一部分:

<span style="float: left">
<a href="?theme=default">def</a>
|
<a href="?theme=black">blk</a>
|
<a href="?theme=blue">blu</a>
</span>

This is my black.css.这是我的黑色。css。

body {
    background-color: #888;
    color: white;
}

My problem is, that the css never seems to go on my JSP file.我的问题是,在我的 JSP 文件中,css 似乎永远不会出现在 go 上。 This is probably caused because properties file can not find css file... Feel free to ask further info.这可能是因为属性文件找不到 css 文件造成的......请随时询问更多信息。

I think there is a problem with this.我认为这有问题。

<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource"> <property name="basenamePrefix" value="resources.theme-" /> </bean>

According to the Javadoc:根据Javadoc:

public void setBasenamePrefix(String basenamePrefix)

Set the prefix that gets applied to the ResourceBundle basenames, ie the theme names.设置应用于 ResourceBundle 基本名称的前缀,即主题名称。 Eg: basenamePrefix="test.", themeName="theme" -> basename="test.theme".例如: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.请注意,ResourceBundle 名称实际上是类路径位置:因此,JDK 的标准 ResourceBundle 将点视为 package 分隔符。 This means that "test.theme" is effectively equivalent to "test/theme", just like it is for programmatic java.util.ResourceBundle usage.这意味着“test.theme”实际上等同于“test/theme”,就像它用于编程 java.util.ResourceBundle 使用一样。

Also, you're allowed to put the theme properties files inside the WEB-INF/classes folder as written in the Reference Docs.此外,您可以将主题属性文件放在参考文档中所写的 WEB-INF/classes 文件夹中。

By default, the ResourceBundleThemeSource uses an empty base name prefix.默认情况下,ResourceBundleThemeSource 使用空的基本名称前缀。 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.因此,您可以将cool.properties 主题定义放在类路径根目录的目录中,例如,在/WEB-INF/classes 中。 The ResourceBundleThemeSource uses the standard Java resource bundle loading mechanism, allowing for full internationalization of themes. ResourceBundleThemeSource 使用标准的 Java 资源包加载机制,允许主题的完全国际化。

I guess the problem in your case is that you've mentioned the path to the css twice.. Once in the <property name="basenamePrefix" value="resources.theme-" /> and once in the properties file too我猜你的问题是你已经两次提到了 css 的路径。一次在<property name="basenamePrefix" value="resources.theme-" />和一次在属性文件中

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

What I needed to do here, was to move my resources(js or css files) to root folder of my project.我需要在这里做的是将我的资源(js 或 css 文件)移动到我项目的根文件夹。

The second thing I could do instead, is to create a resource mapping for my css files.我可以做的第二件事是为我的 css 文件创建资源映射。

Both would work, but I went with the first solution at the moment.两者都可以,但我现在选择了第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM