What You'll Learn

We want to offer a URL to access the profile page for a single charity. We've already planned for this in the charity list where all of the charities created links to /charity/{the furl of the charity}.

e.g. /charity/rspca will take us to the page for the RSPCA. This means that the acronym is acting like a furl (friendly URL).

Let's amend the controller by adding a new method to handle a request for a specific charity's page.

//code omitted
@GetMapping("/charity/{furl}")
    public String showAllCharities(@PathVariable(value = "furl", required = true) String acronym, Model model) {
        Optional<CharityDTO> charityDTO;
        charityDTO = charitySearch.findByAcronym(acronym);

        if (charityDTO.isPresent()) {
            model.addAttribute("charity", charityDTO.get());
            return "charity-home";
        } else {
            throw new CharityNotFoundException("No charity present at that address");
        }
    }

Add a new template (you should know where to add this now) for the charity home. There are no new Thymeleaf attributes in this. The main div in the page looks like this:

<div class="container">

    <h1 th:text="${charity.name}"></h1>

    <div th:text="${charity.missionStatement}">More charity information to go here...</div>

    <div th:text="'Registration: ' + ${charity.registration}"></div>

</div>

The controller needs to get the single charity DTO object, so we add methods to the service and repository layers to enable this. These are not shown in the tutorial, but are included in the tag.

To test this, run gradle > build > classes again and then go to http://localhost:8080/charity/rspca.

Now, try the following:

Notice the error handling. We can do better...and Spring helps us.

In this tutorial, we've introduced the notion of a path variable and how to deal with variations in the URL.

You can combine query parameters and path variables and you can many of each in the same URL.

We are partially handling a request for a missing charity, but the user interface isn't good.

Checkout tag 013-add-404 and examine the differences (there's a new HTML file in /templates/errors) and test the system again. Spring Boot will look for different error templates automatically depending on the status code that is thrown. A generic error.html file can also be used.