Martin Erlic
Martin Erlic

Reputation: 5667

Model Boolean attribute value not showing in HTML (Spring MVC)

Controller:

@RequestMapping("/approveRequestById")
    public String approveRequestById(Principal principal, @RequestParam(value="id") String requestId, Model model) {
        Users manager = usersRepository.findOneByInitialName(principal.getName());
        RequestDO request = requestRepository.findOne(Long.parseLong(requestId));
        requestRepository.updateRequestStatusByRequestId(RequestStatus.APPROVED, request.getId());
        Users employee = usersRepository.findOne(request.getUsers().getId());
        // Instead of getting the same RequestDO object from DB, I just updated it's status for using in mail correctly.
        request.setStatus(RequestStatus.APPROVED);

        model.addAttribute("requestFlag", true);
        log.info("Model: " + String.valueOf(model));

        /***
         Send Notification Mail to Employee
         ***/
        mailUtil.sendNotificationEmailWithTemplating(employee, manager, request);

        return "redirect:/requests";
    }

HTML:

<th:block th:switch="${requestFlag}">
                    <th:block th:case="true">
                        <div style="width: 100%; height: 30px; color: #fff; background: #C00000; text-align: center; padding: 10px;"><span
                                style="font-weight: bold; padding-bottom: 10px; ">The request from your mailbox has been approved!</span></div><br>
                    </th:block>
                </th:block>

Log output when landing on page after email link:

Model: {currentRole=EMPLOYEE, numRequests=0, requestFlag=true}

The user accesses a page from an email ink, i.e. http://localhost:8181/request/approveRequestById?id=2. Once on this page, the controller inserts the model attribute requestFlag and sets it to true. I can see in both the log and debug mode that the model does in fact add this attribute to the model when the request mapping is called, i.e. /request/approveRequestById.

My problem is that the logic is not working on the front-end. The div does not show. If there is a requestFlag equal to true, it should show a div. I can print the value using <th:block th:text="@{requestFlag}">Request Flag</th:block> but it doesn't give me the boolean value I want. Can Booleans not be rendered via Thymeleaf? The other strings in the included model print just fine. Am I missing something fundamental in terms of the order of how templates are rendered? All help greatly appreciated.

Upvotes: 1

Views: 3186

Answers (1)

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

Since you are using redirect: your model will be lost with redirecting (a new request will be created) so you have to store your data in redirectAttributes.

@RequestMapping("/approveRequestById")
public String approveRequestById(Principal principal, @RequestParam(value="id") String requestId, Model model, RedirectAttributes redirectAttributes) {

        //use this
        redirectAttributes.addFlashAttribute("requestFlag", true);

        //instead of
        model.addAttribute("requestFlag", true);

        return "redirect:/requests";
    }

Upvotes: 1

Related Questions