SpringBoot,Thymeleaf,Gradle:不访问静态资源,css没有显示

问题描述 投票:0回答:1

此时我已经尝试了很多东西,但我的CSS不会显示在我的页面上。这是我的百里香页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8"/>
    <title th:text="${title}">Default Title</title>
    <link th:href="@{/css/styles.css}" rel="stylesheet" type="text/css" />

</head>

这是我加载此页面时出现的错误,我认为这很重要:

**2018-04-11 05:57:08.425  WARN 7904 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/css/styles.css] in DispatcherServlet with name 'dispatcherServlet'**

我在PROPER文件夹中有我的静态资源,如下所示(图片链接):

Folders path sources> main> resources> static,然后是另一个css文件夹,我确实将其添加到@ {}。

我搜索并搜索到处寻找答案,我甚至将其添加到我的WebMcvConfigurerAdapter类中,就像我一直看到的那样:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
}

这是我现在的(我使用拦截器和东西,只是忽略那部分):

@Configuration
@EnableWebMvc
public class MyAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new ProjectManagerInterceptor());
    }
}

这是我的build.gradle文件,如果它有帮助:

buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'org.launchcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.session:spring-session')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

bootRun {
    addResources = true
}

我有另一个项目也使用静态资源,就像我正在尝试做的那样,并且它完美地工作。我将发布一些代码和信息,以防它可能有所帮助。

工作申请的信息

当我用静态资源加载页面时的控制台:

2018-04-11 05:53:15.553 DEBUG 7448 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/css/bootstrap.css]
2018-04-11 05:53:15.554 DEBUG 7448 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /css/bootstrap.css
2018-04-11 05:53:15.554 DEBUG 7448 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/css/techjobs.css]
2018-04-11 05:53:15.554 DEBUG 7448 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /css/techjobs.css
2018-04-11 05:53:15.556 DEBUG 7448 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/css/bootstrap.css]
2018-04-11 05:53:15.556 DEBUG 7448 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/css/bootstrap.css] are [/**]
2018-04-11 05:53:15.558 DEBUG 7448 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/css/bootstrap.css] are {}
2018-04-11 05:53:15.559 DEBUG 7448 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/css/bootstrap.css] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@3f0bd4d8]]] and 1 interceptor
2018-04-11 05:53:15.560 DEBUG 7448 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/css/bootstrap.css] is: -1
2018-04-11 05:53:15.580 DEBUG 7448 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/js/bootstrap.js]
2018-04-11 05:53:15.581 DEBUG 7448 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /js/bootstrap.js
2018-04-11 05:53:15.582 DEBUG 7448 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/js/bootstrap.js]
2018-04-11 05:53:15.582 DEBUG 7448 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/js/bootstrap.js] are [/**]
2018-04-11 05:53:15.582 DEBUG 7448 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/js/bootstrap.js] are {}
2018-04-11 05:53:15.582 DEBUG 7448 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/js/bootstrap.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@3f0bd4d8]]] and 1 interceptor
2018-04-11 05:53:15.582 DEBUG 7448 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/js/bootstrap.js] is: -1
2018-04-11 05:53:15.592 DEBUG 7448 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/css/techjobs.css]
2018-04-11 05:53:15.594 DEBUG 7448 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/css/techjobs.css] are [/**]
2018-04-11 05:53:15.595 DEBUG 7448 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/css/techjobs.css] are {}
2018-04-11 05:53:15.595 DEBUG 7448 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/css/techjobs.css] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@3f0bd4d8]]] and 1 interceptor
2018-04-11 05:53:15.595 DEBUG 7448 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/css/techjobs.css] is: -1

HTML:

<head th:fragment="head">

    <!-- Bootstrap stylesheets and script -->
    <link th:href="@{/css/bootstrap.css}" rel="stylesheet" />
    <link th:href="@{/css/techjobs.css}" rel="stylesheet" />
    <script type="text/javascript" th:src="@{/js/bootstrap.js}"></script>

    <title th:text="'TechJobs' + ${title == null ? '' : ' :: ' + title}">TechJobs</title>
</head>

文件夹设置:Working app's folder paths

如果有任何问题,请不要犹豫!我真的很沮丧,这么多时间

spring-boot gradle static resources thymeleaf
1个回答
0
投票

我刚刚找到了自己问题的答案,所以我会留下这个听到任何可能找到它的人。

Spring Boot not serving static content

我有一个不必要的@EnableWebMvc注释,我拿出来,我的资源有效:D!

© www.soinside.com 2019 - 2024. All rights reserved.