java - Spring Boot thymeleaf 无法加载 css 资源

标签 java spring spring-boot thymeleaf

我可以看到我的引导模板使用 thymeleaf 和 Spring Boot。

当我请求 http://localhost:8080/student 引导资源工作良好,但是当我请求包含一些路径变量时,如 http://localhost:8080/student/3 bootstrap 无法上传。我从浏览器请求方法:GET 收到此错误 状态代码:400。它尝试请求像 http://localhost:8080/student/bootstrap.css 这样的引导文件。

我认为声明引导文件的路径没有任何问题,因为其他页面工作正常。 我的路径来自资源/静态/bootstrap.css 指南 我没有使用 WebMvcConfig,而是使用默认的 spring 安全性。

@RequestMapping( value = "/student/{id}", method = RequestMethod.GET)
public String izinEkle(Model model, @PathVariable int id) {


    studentService.getStudentlById(id);
    Izin izin = new Izin();
    model.addAttribute("izin",izin);
    return "izinGiris";
}

--

<head lang="en">

<title>HolyDayTracker</title>
<link href="../static/bootstrap.css" th:href="@{bootstrap.css}" rel="stylesheet" media="screen">
<link href="../static/style.css" th:href="@{style.css}" rel="stylesheet">
<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js   th:src="@{../webjars/jquery/2.1.4/jquery.min.js}"></script>

最佳答案

尝试在 thymeleaf href attr 值中添加前导斜杠/。并且您不需要额外的 href 属性,只需 th:href 即可。

<link th:href="@{/bootstrap.css}" rel="stylesheet" media="screen">
<link th:href="@{/style.css}" rel="stylesheet">

关于java - Spring Boot thymeleaf 无法加载 css 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42402259/

相关文章:

Java线程安全地将集合对象从一个线程传递到另一个线程

java - Java输入数字求平均值

spring - 尝试找到一种更语义化的方式在 Kotlin 中编写 Spring Entity

spring - Grails - 在 Spring 3.1 的 @Cacheable 中使用 Spring el 表达式

java - Spring Boot 的外部库文件夹

java - 将文件夹恢复到包中

java - 同步 Controller 与 View

java - 如何在 Java 中通过反射调用代理(Spring AOP)上的方法?

spring-boot - Hystrix 和 Turbine 不适用于 Spring boot 2 和 Spring cloud Finchley.M8

java - RestRepositoryResource 的自动配置