【发布时间】:2019-11-13 23:54:40
【问题描述】:
我正在使用 Thymeleaf 构建 Spring Boot MVC 应用程序,但在将静态内容(例如 CSS)加载到页面时遇到问题。
每当我尝试在模板中加载 css 文件时,请求都会失败,错误代码为 405 Request method 'GET' not supported。
我尝试了多种不同的方式来引用 css 文件,例如
<link href="/css/style.css}" rel="stylesheet" type="text/css"/>
<link href="../static/css/style.css}" rel="stylesheet" type="text/css"/>
<link href="/static/css/style.css}" rel="stylesheet" type="text/css"/>
<link th:href="@{/static/css/style.css}" rel="stylesheet" type="text/css"/>
基本上所有前面所有可能的组合,仍然得到相同的错误。
我的文件结构如下:
└── src
└── main
├── java
│ └── cz
│ └── company
│ └── nps
│ ├── NpsApplication.java
│ ├── WebConfig.java
│ └── controller
│ └── SurveyController.java
└── resources
├── application.properties
├── messages.properties
├── messages_CZ.properties
├── messages_EN.properties
├── static
│ ├── css
│ │ └── style.css
│ ├── img
│ │ └── favicon.ico
│ └── js
└── templates
├── footer.html
├── header.html
└── survey.html
WebCofig.java:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Bean
public LocaleResolver localeResolver()
{
final SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("cs", "CZ"));
return localeResolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
我做错了什么?根据我找到的所有材料,这应该没有任何问题。
编辑 1: build.gradle:
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'cz.company'
sourceCompatibility = '11'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.webjars:bootstrap:3.3.7'
implementation 'dom4j:dom4j:1.6.1'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2'
implementation 'javax.jws:javax.jws-api:1.1'
implementation 'javax.xml.ws:jaxws-api:2.3.1'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
【问题讨论】:
标签: css spring-boot spring-mvc bootstrap-4 thymeleaf