我正在为我的元素使用 spring,并且我将所有模板文件排除到另一个“html 元素”。我这样做是因为我可以在没有任何重新部署的情况下进行更改,并且对我来说它的方式更清晰。它工作正常,spring 应用程序找到所有模板。但是现在,模板找不到任何 css 或 js 文件。该模板尝试在我的 spring-project 中搜索文件,但它们包含在 html 元素中。我该如何解决?我认为我需要类似相对路径的东西。
元素
SpringApp
---- 库
---- 来源
-------- 主要
-------------- java
-------------- com.example.test
------------------ Controller
---------------------- ShowIndex.java
---------- 资源
---------- 网络应用程序
-------- 测试
HTML 模板
---- 库
---- 来源
-------- 主要
-------------- java
---------- 资源
---------- 模板
-------------- CSS
------------------ bootstrap.min.css
-------------- js
------------------ bootstrap.min.js
-------------- 图片
-------------- 索引.ftl
-------------- 测试.ftl
-------- 测试
ShowIndex.java 的内容
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(ModelMap model) {
model.put("prename", "Hans-Peter");
return "index";
}
index.ftl的HeaderContent
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/zeus.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
有什么想法吗?
我使用:
java
Spring WebMVC
Freemarker 作为模板引擎
最佳答案
把你的css
, js
, images
放到src/main/webapps
文件夹下,与模板 ftl
文件。
编辑
对于 Servlet 3.0,您有另一种选择,但可能无法在所有容器中工作...
- 创建一个
META-INF/resources
文件夹 - 把你所有的资源
js, css, images
等放到这个文件夹 - 并将其“装入”。
例如,我创建了一个 assembly.xml
文件来自动完成这项工作
<assembly>
<id>bin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>**</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<excludes>
<exclude>**/DefaultController.class</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>WebContent</directory>
<outputDirectory>META-INF/resources</outputDirectory>
<includes>
<include>/WEB-INF/jsp/**</include>
</includes>
</fileSet>
<fileSet>
<directory>WebContent</directory>
<outputDirectory>META-INF/resources</outputDirectory>
<includes>
<include>js/**</include>
<include>plugins/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
此文件在使用 maven 构建 mvn package
时有效。
附言:
在 maven pom.xml
中:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
关于javascript - 找不到带有排除模板 JAVA + Spring 的 CSS 和 JS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101580/