Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 344
Saikat Avatar asked Mar 06 '23 00:03

Saikat


2 Answers

(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}">
like image 138
Saikat Avatar answered Apr 07 '23 14:04

Saikat


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
}
like image 21
Sumit Avatar answered Apr 07 '23 12:04

Sumit