springboot + thymeleaf static resource access 404

When using static resource reference springboot thtmeleaf development and 404, static resources are summarized below:

 

 

 index.html file:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset = "UFT-8" />
    <title>Spring Boot Application</title>
    <link href="css/style.css" rel="stylesheet">
    <!--<link th:href="@{css/style.css}" rel="stylesheet">-->
</head>
<body>
<is available for purchase to the Boot Thymeleaf the Spring Web garbled garbled the Application>h4</h4>
<p th:text="${hello}">hello</p>
<p th:text="${hi}">hi</p>
<input type="text" />
</body>
</html>

style.css file

h4 {
    color: red;
}

p {
    color: blue;
}

Test Access url

@Controller
@RequestMapping(value = "thymeleaf")
public class IndexController {

    @RequestMapping(value = "index")
    public String index(Model model, ModelMap modelMap) {

        model.addAttribute("hello", "thymeleaf");

        modelMap.addAttribute("hi", "thymeleaf");

        return "index";
    }

    @RequestMapping(value = "hello")
    public String hello(ModelMap modelMap) {

        modelMap.put("hei", "thymeleaf");

        return "hello";
    }
}

Application configuration file

#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.mode=HTML

After the visit to start the project http://127.0.0.1:8080/thymeleaf/index , writing style did not introduce the project, open the console found add character to remove background url request url final thought on static resource access string (if it is / thymeleaf / index / hello will be http://127.0.0.1:8080/thymeleaf/index/css/style.css), obviously this is not a static resource access position, 404 also normal.

 

 

 And direct access to the http://127.0.0.1:8080/css/style.css is ok.

 

 The problem is that the English import static resource file path to write the wrong, should be written in the introduction index.html

<link href="/css/style.css" rel="stylesheet">
<!--<link th:href="@{/css/style.css}" rel="stylesheet">-->

Plus "/" indicates an absolute path

Guess you like

Origin www.cnblogs.com/kingsonfu/p/11516967.html
Recommended