Spring 4加载静态资源 [英] Spring 4 loading static resources

查看:182
本文介绍了Spring 4加载静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹簧MVC应用程序,其中有一堆css和js文件,目前放在 src / main / java / resources / assets 目录中。

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文档和一些教程,介绍如何使用 ResourceHandlerRegistry 类。我特别认为,来自本教程完全适合我的项目结构。

I read through the Spring Docs and some Tutorials on how to load these files for my templates using the ResourceHandlerRegistry class. I especially thought that the code snippets from this Tutorial would perfectly fit my project structure.

但我总是在我的资源文件上有404。

But I get always a 404 on my resource files.

这是我当前运行的应用程序/配置类:

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/");

    }

}

HEADer用于我的HTML文件(放置在资源/模板中):

And here a HEADer used in my HTML files (placed in resources/templates):

<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>

html文件通过我的控制器类正确加载,我的 style.css 文件( http:// localhost:8080 / css / style.css )我得到一个404如上所述。

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.

我似乎没有找到任何更多的资源,谁可以提供我更多关于这个主题的信息Spring 4。我错过了一些配置文件?或者不是资源处理程序注册适合我的结构?我期待您的回应。

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.

推荐答案

您说您的样式表和JavaScript文件在/ assets下。我将假设你有目录/ assets / css和/ assets / js。然后,给定以下资源处理程序定义:

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:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("/assets/**")
    .addResourceLocations("classpath:/assets/");
}

您将在HTML中加载以下资源:

You would load these resources in your HTML like so:

<link href="/assets/css/style.css" rel="stylesheet" />
<script src="/assets/js/general.js"></script>

这篇关于Spring 4加载静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆