Spring Boot 不加载静态资源 [英] Spring Boot not loading static resources

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

问题描述

有很多关于 spring boot 没有加载静态资源的问题,并且已经阅读了所有(几乎)我仍然无法解决这个问题.在这个阶段,我选择不使用 spring boot 运行,但我仍然想知道问题是什么.我正在使用 Eclipse、Java 8 和 Maven.

There are loads of questions about spring boot not loading static resources and having read them all (almost) I still can't fix this issue. At this stage I have opted not to run with spring boot but I'd still like to know what the issue was. I am using Eclipse, Java 8 and Maven.

我有一个看起来像这样的应用程序类:

I have an application class that looks like this:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我创建了一个 css 文件 - src/main/resources/static/style.css并从一个jsp引用了这个:

I have created a css file - src/main/resources/static/style.css and referenced this from a jsp:

<link href="<c:url value='style.css'/>" rel="stylesheet">

页面加载但css不加载.这是错误 - 405 Method Not Allowed

The page loads but not the css. This is the error - 405 Method Not Allowed

我认为是正确的但不确定.感谢所有帮助.

I think think is correct but not sure. All help appreciated.

根据下面的一些评论,这就是现在的样子.

我的jsp文件在src/main/resources/application.properties中配置如下:

My jsp files are configured in src/main/resources/application.properties as follows:

spring.mvc.view.prefix:/WEB-INF/views/
spring.mvc.view.suffix:.jsp

我的jsp很简单,位于/WEB-INF/views/home.jsp

My Jsp is very simple, and is located in /WEB-INF/views/home.jsp

<!DOCTYPE html>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <link href="public/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
    <p>Hello world!</p>
</body>
</html>

我也试过像这样链接我的 css 文件:

I have also tried linking my css file like this:

<link href="style.css" rel="stylesheet" type="text/css"/>

我的css文件,位于webapp/public/style.css,也很简单

My css file, located in webapp/public/style.css, is also very simple

p {
    color: red;
}

我的jsp加载了但css没有加载.我使用各种方法运行我的应用程序,包括:

My jsp loads but not the css. I have run my application using various methods including:

从命令行 - java -jar contacts.jar

From the command line - java -jar contacts.jar

在 Eclipse 中 - mvn spring-boot:run 和 mvn tomcat7:run-war同样在 eclipse 中通过右键单击 Application.class 文件并选择运行方式 -> Java 应用程序.

Inside eclipse - mvn spring-boot:run and mvn tomcat7:run-war Also inside eclipse by right clicking the Application.class file and selecting Run As -> Java Application.

我使用的是 Spring Boot 版本 1.4.0.RELEASE

I am using Spring Boot Version 1.4.0.RELEASE

推荐答案

CSS 位置

将您的静态资源(例如 css)放在 src/main/resources 之外,后者用于应用程序资源,例如属性文件.

CSS Location

Put your static resources such as css outside of src/main/resources, which is used for application resources such as properties files.

我总是把 css 文件放在 src/main/webapp/assets/css 文件夹下.

I always put css files under src/main/webapp/assets/css folder.

我使用的是 Java 配置而不是 XML,以下代码段向您展示了如何配置 Spring Boot 以识别和查找 css.

I am using Java config instead of XML, the following snippet shows you how to configure spring boot to recognize and find the css.

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{    

    // view resolver and other confugurations ommitted...

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

访问 css

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

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