【问题标题】:Spring MVC and Thymeleaf - only classpath mapping worksSpring MVC 和 Thymeleaf - 只有类路径映射有效
【发布时间】:2019-01-02 10:19:38
【问题描述】:

我想知道为什么我必须在 Spring 的资源位置映射中添加“类路径:”,这是最佳做法吗?

目前我的项目是这样设置的,但是如果我删除“类路径”,它什么也找不到(而且我也无法提供正确的直接链接)。为了集成引导程序,我什至不得不使用“webjars-locator”来使 webjars 可访问(由于缺少透明度)。

谁能解释一下为什么需要类路径(并且没有直接链接),以及如何找到或重新构建项目以使其正常工作?

资源处理程序: @Override

public void addResourceHandlers(final ResourceHandlerRegistry registry) {

        registry.addResourceHandler(
                "/webjars/**",
                "/img/**",
                "/css/**",
                "/js/**")
        .addResourceLocations(
                "classpath:/META-INF/resources/webjars/",
                "classpath:/static/img/",
                "classpath:/static/css/",
                        "classpath:/static/js/");

    }

index.htm:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>

<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/4.2.1/css/bootstrap.min.css}" />

</head>
<body>
    <h1 class="color1">Hello, world! style</h1>

    <p>
        Click <a th:href="@{/hello}">here</a> to see a greeting.
    </p>


    <!-- include javascript in the footer -->
    **<script type="text/javascript" th:src="@{/webjars/jquery/3.2.1/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/webjars/bootstrap/4.2.1/js/bootstrap.min.js}"></script>**
</body>
</html>

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

    使用类路径是正常的。如果您想从根路径额外加载资源,请使用

    {"/", "classpath:/META-INF/resources/webjars/" ...}
    

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 2019-07-11
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多