How to link another html page in Spring Boot Thymeleaf Template Engine?

14,071

Solution 1

(As JBNizet pointed out in the comment) As per MVC design architecture it's better to use controller to render the views instead of view-to-view links. All I had to do was update my Controller class with:

@RequestMapping("/defect-details")
public String defectDetails() {
    return "defect-details";
}

And in the Thymeleaf template:

<a th:href="@{defect-details}">

Solution 2

You can always use HTML tag to link two pages in Thymeleaf, However you wont be able to go to a specific HTML page from one HTML page directly without going through spring controller first. You have to make a request to Spring Controller to get that page for you. In order to get it done :--

1.) <a href="defect-details.html"> //on your index.html

2.) Get this request on your Spring Controller to open defect-details.html page for you :--

@RequestMapping("/defect-details")
public String defectDetails() {
    return "defect-details"; //defect-details.html page name to open it
}
Share:
14,071
Saikat
Author by

Saikat

... and while writing the code visit StackOverflow ;)

Updated on July 07, 2022

Comments

  • Saikat
    Saikat almost 2 years

    I have following Spring Boot project structure (using Thymeleaf) -

    enter image description here

    Now, when I tried to reference defect-details.html from index.html it could not be found. I tried all the following options to no avail:

    1.<a th:href="@{/defect-details.html}">

    2.<a th:href="@{defect-details.html}">

    3.<a href="defect-details.html">

    Every time it says There was an unexpected error (type=Not Found, status=404).

    Please help to find the issue.

  • Ponnaveen S
    Ponnaveen S about 4 years
    Can you explain me, what exactly happens when we use controller to render the views instead of view-to-view links?