我在我的项目中使用spring,我将所有模板文件排除到另一个"html- project“中。我这么做是因为我可以在没有任何调动的情况下做一些改变,而且对我来说,它的方式更清晰。它工作得很好,spring应用程序可以找到所有模板。但是现在,模板找不到任何css或js文件。模板试图搜索我的spring项目中的文件,但它们包含在html项目中。我怎么才能修好它?我想我需要一条相对的路。
项目
SpringApp
-主要
-爪哇
- com.example.test
-控制器
- ShowIndex.java
--资源-资源
- webapp
-测试
HTML-Templates
-主要
-爪哇
--资源-资源
-模板
- bootstrap.min.css
- bootstrap.min.js
-图像
- index.ftl
- test.ftl
-测试
ShowIndex.java含量
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(ModelMap model) {
model.put("prename", "Hans-Peter");
return "index";
}HeaderContent of index.ftl
<!-- 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
弹簧WebMVC
共济会成员为TemplateEngine
发布于 2017-03-30 05:06:50
将您的css、js、images放在src/main/webapps文件夹中,与模板ftl文件分开。
编辑
使用Servlet3.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时,该文件可以工作。
PS
在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>https://stackoverflow.com/questions/43101580
复制相似问题