Spring Boot and Thymeleaf - “a href”s to another sites going into 404 error

若如初见. 提交于 2020-05-17 06:07:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!