簡體   English   中英

Spring MVC和Thymeleaf-僅類路徑映射有效

[英]Spring MVC and Thymeleaf - only classpath mapping works

我想知道為什么我必須總是在Spring的資源位置映射中添加“ classpath:”,這是最佳實踐嗎?

目前,我的項目是這樣設置的,但是如果我刪除“ classpath”,它將找不到任何東西(而且我也無法提供適當的直接鏈接)。 對於完整的引導程序,我什至不得不使用“ webjars-locator”來使webjar可以訪問(由於缺少透明度)。

誰能解釋,為什么需要類路徑(並且沒有直接鏈接),以及如何找到或重新構建項目以使其正常工作?

ResourceHandlers:@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>

項目結構

使用類路徑是正常的。 如果要從根路徑額外加載資源,請使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM