Saikat
Saikat

Reputation: 16790

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.

Upvotes: 8

Views: 24258

Answers (3)

Shubham Ray
Shubham Ray

Reputation: 23

You can use ModelAndView class also:

@RequestMapping("/defect-details")
public ModelAndView defect-details(){
    ModelAndView mv = new ModelAndView();
    mv.setViewName("defect-details");
    return mv;
}

Upvotes: 0

Sumit
Sumit

Reputation: 957

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
}

Upvotes: 2

Saikat
Saikat

Reputation: 16790

(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}">

Upvotes: 10

Related Questions