Java Spring 4 加载静态资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24080025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 4 loading static resources
提问by SakeSushiBig
I got a spring MVC application with a bunch of css and js files currently placed in the src/main/java/resources/assets
directory.
我得到了一个 spring MVC 应用程序,其中包含当前放置在src/main/java/resources/assets
目录中的一堆 css 和 js 文件。
I read through the Spring Docs and some Tutorials on how to load these files for my templates using the ResourceHandlerRegistryclass. I especially thought that the code snippets from this Tutorialwould perfectly fit my project structure.
我通读了 Spring Docs 和一些关于如何使用ResourceHandlerRegistry类为我的模板加载这些文件的教程。我特别认为本教程中的代码片段非常适合我的项目结构。
But I get always a 404 on my resource files.
但是我的资源文件总是出现 404。
Here is the Application/Configuration class I'm currently running with:
这是我目前正在运行的应用程序/配置类:
@Configuration
@EnableAutoConfiguration
@ImportResource("/applicationContext.xml") // only used for jpa/hibernate
@EnableWebMvc
@ComponentScan(basePackages = "at.sustain.docutools.viewer.presentation")
public class Application extends WebMvcConfigurerAdapter {
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/assets/");
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/");
registry.addResourceHandler("/js/**")
.addResourceLocations("/js/");
}
}
And here a HEADer used in my HTML files (placed in resources/templates):
这里是我的 HTML 文件中使用的 HEADer(放置在资源/模板中):
<head>
<!-- local Stylesheet -->
<link href="css/style.css" rel="stylesheet" />
<!-- local Javascript files -->
<script src="js/general.js"></script>
<script src="js/xmlhttp.js"></script>
<!-- local Javascript libraries -->
<script src="js/lib/filter.js"></script>
<script src="js/lib/jquery.fs.zoomer.js"></script>
<script src="js/lib/jquery.validate.js"></script>
</head>
The html file is loaded correctly through my controller classes but when trying to hit e.g. my style.css
file (http://localhost:8080/css/style.css
) I get an 404 as already mentioned.
html 文件通过我的控制器类正确加载,但是当我尝试点击我的style.css
文件 ( http://localhost:8080/css/style.css
) 时,我得到了一个 404,如前所述。
I can't seem to find any more resources who could provide me with more information on this subject for Spring 4. Do I miss some configuration files? Or aren't the resource handler registrations fitting my structure? I'm looking forward to your responses.
我似乎找不到更多可以为我提供有关 Spring 4 主题的更多信息的资源。我是否错过了一些配置文件?或者资源处理程序注册不适合我的结构?我期待着您的回复。
采纳答案by James Sumners
You say that your stylesheets and JavaScript files are under "/assets". I'm going to assume you have directories "/assets/css" and "/assets/js". Then, given the following resource handler definition:
您说您的样式表和 JavaScript 文件位于“/assets”下。我将假设您有目录“/assets/css”和“/assets/js”。然后,给定以下资源处理程序定义:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/assets/");
}
You would load these resources in your HTML like so:
您可以像这样在 HTML 中加载这些资源:
<link href="/assets/css/style.css" rel="stylesheet" />
<script src="/assets/js/general.js"></script>