问题
I have created a spring-boot-starter-thymeleaf project version 2.2.6-RELEASE with IntelliJ version 2020.1. Unfortunately, I do not know why the links in the thymeleaf templates are not referring to the next ones.
When I start the project and type "localhost:8080" in my web browser the index.html site is visible. After clicking a "a href" inside the index.html , the web browser shows me the url e.g. "localhost:8080/product.html" with an 404 error.
Heres the project structure: Project structure
The index.html file here:
<!DOCTYPE html>
<html xmlns:th ="http://www.thymeleaf.org" lang="de">
<head>
<meta charset="UTF-8">
<title>Naturkost Warenmanagement</title>
<link th:href="@{/stylesheet.css}" rel="stylesheet" />
</head>
<body>
<div id="container">
<div id="header">
<h1>Warenmanagement</h1>
<h2>Geschenke und Naturkost</h2>
</div>
<div id="content">
<div id="nav">
<h3>Navigation</h3>
<ul>
<li><a th:href="@{/index.html}">Home</a></li>
<li><a th:href="@{/product.html}">Artikel</a></li>
<li><a th:href="@{/employee.html}">Mitarbeiter</a></li>
<li><a th:href="@{/order.html}">Bestellung</a></li>
<li><a th:href="@{/stockground.html}">Lager</a></li>
<li><a th:href="@{/supplier.html}">Lieferant</a></li>
<li><a th:href="@{/transaction.html}">Lager Transaktion</a></li>
</ul>
</div>
.
.
.
.
@Controller NavigationController.class:
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/employee")
public String employee(){
return "employee";
}
@RequestMapping("/order")
public String order(){
return "order";
}
@RequestMapping("/product")
public String product(){
return "product";
}
@RequestMapping("/stockground")
public String stockground(){
return "stockground";
}
@RequestMapping("/supplier")
public String supplier(){
return "supplier";
}
@RequestMapping("/transaction")
public String transaction(){
return "transaction";
}
What am I doing wrong here?
回答1:
Remove .html from action:
<li><a th:href="@{/index.html}">Home</a></li>
And keep it like :
<li><a th:href="@{/index}">Home</a></li>
Now it will go and hit API - localhost:8080/<action>
And respective .html will be added by spring boot using view resolver.
来源:https://stackoverflow.com/questions/61700622/spring-boot-and-thymeleaf-a-hrefs-to-another-sites-going-into-404-error