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

Posted

技术标签:

【中文标题】SpringBoot、Thymeleaf、Gradle:不访问静态资源,css 不显示【英文标题】:SpringBoot, Thymeleaf, Gradle: not accessing static resources, css not showing 【发布时间】:2018-09-21 06:18:42 【问题描述】:

此时我已经尝试了很多东西,但我的 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

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

【问题讨论】:

【参考方案1】:

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

Spring Boot not serving static content

我取出了一个不必要的 @EnableWebMvc 注释,我的资源正常工作:D!

【讨论】:

以上是关于SpringBoot、Thymeleaf、Gradle:不访问静态资源,css 不显示的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot——SpringBoot集成Thymeleaf

springboot 中使用thymeleaf

SpringBoot系列之集成Thymeleaf用法手册

SpringBoot :thymeleaf 使用详解

springboot-thymeleaf

springboot:thymeleaf使用详解