0

I have a little problem when I push a button on my page that should just get a template and display it, it doesn't happen but the actual template name is printed on a white page in browser. I have a completely same controller set up and template for a different objects and it works. I am working with Java, Spring Boot and Thymeleaf.

This is my controller in question. The controller class path is /family.

@GetMapping("/all")
public String getFamilyList(Model model) {
    List<Family> list = familyService.getAllFamilies();
    model.addAttribute("families", list);
    return "all-families";
}

This is the button from the template that works fine

<a th:href="@{/family/all}" class="btn btn-success">Family list</a>

This is the all-families template

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>List of all families</title>
<link rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
    integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
    crossorigin="anonymous">
<link rel="stylesheet" th:href="@{/css/main.css}" />
</head>

<body>
    <div class="navbar navbar-expand-lg static-top">
        <a class="navbar-brand" href="/"> <span class="sr-only">(current)</span>
            <img th:src="@{/images/plantIcon.png}" alt="..." height="36" />
        </a>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/">Home<span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/herbarium/all/user">Herbarium</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/userLists/user/{id}(id=${current.userId})">Lists</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/family/search">Find</a>
                </li>
            </ul>
        </div>
    </div>

    <div class="container">
        <a th:href="@{/family}" class="btn btn-success">Add Family</a>
        <br></br>
        <table class="table">
            <thead>
                <tr>
                    <th scope="col">Name</th>
                    <th scope="col">Info link</th>
                    <th scope="col" colspan="2"></th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="family: ${families}">
                    <td th:text="${family.name}"></td>
                    <td th:text="${family.link}"></td>
                    <td>
                        <a th:href="@{/family/update/{id}(id=${family.familyId})}" class="btn btn-info">Update family</a>
                    </td>
                    <td>
                        <a th:href="@{/family/delete/{id}(id=${family.familyId})}" class="btn btn-danger">Delete family</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <footer id="sticky-footer" class="flex-shrink-0 py-4 text-white-50">
        <div>
            <small>Copyright &copy; Online Herbarium</small>
        </div>
    </footer>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script
        src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script
        src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
    <script
        src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
</body>
</html>
Faheem azaz Bhanej
  • 2,328
  • 3
  • 14
  • 29
  • Please specify clearly what you have to do and where you getting a problem – Faheem azaz Bhanej Aug 20 '22 at 10:50
  • When I start up my page and login as admin I have a button that activates the above controller. The controller is supposed to open up a template with the data provided in the browser. The problem is it does not open it and it doesn't give any type of error. On a blank page is literally typed out all-families which is the name of the template. – Deni Bakulić Aug 20 '22 at 11:10
  • Could you show the complete code for the controller? You might have marked it `@RestController` or `@ResponseBody`. – Unmitigated Aug 20 '22 at 13:56
  • Although it is random but my answer below fixed the issues. Regarding your question I did check that before and it is @Controller – Deni Bakulić Aug 21 '22 at 10:18

1 Answers1

0

I was able to fix the issue by copying the code into a new class and changing the class request mapping path from /family to /families. For some reason /family just doesn't work.