此时我已经尝试了很多东西,但我的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 来源&gt;主要&gt;资源&gt;静态,然后另一个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
如果有任何问题,请不要犹豫!我真的很沮丧,这么多时间
答案 0 :(得分:0)
我刚刚找到了我自己的问题的答案,所以我会留下听到任何可能找到它的人。
Spring Boot not serving static content
我有一个不必要的@EnableWebMvc注释,我拿出来了,我的资源有效:D!