How can I show custom error messages using Thymeleaf?

Metehan Altuntas

I'm trying to do CRUD operations with Spring. I'm using HTML and Thymeleaf on the frontend. I return the results of certain actions I have made and error messages, if any, using a custom class that I write. I don't have any problems so far. However, if an error occurs during these operations and I return this error through the class I wrote, I do not know how to display it on HTML using Thymeleaf.

I'm returning an object of this class type;

@Getter
@Setter
public class WarehouseAPIResponseHolder<T> {

    private T responseData;
    private HttpStatus httpStatus;
    private WarehouseAPIResponseError error;

    public WarehouseAPIResponseHolder(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public WarehouseAPIResponseHolder(T responseData, HttpStatus httpStatus) {
        this.responseData = responseData;
        this.httpStatus = httpStatus;
    }

    public WarehouseAPIResponseHolder(HttpStatus httpStatus,
                                      WarehouseAPIResponseError error) {
        this.httpStatus = httpStatus;
        this.error = error;
    }

}

My error class;

@Getter
@Builder
public class WarehouseAPIResponseError {

    private String code;
    private String message;

}

Example of an error;

if (CollectionUtils.isEmpty(warehouseEntities)) {
            return new WarehouseAPIResponseHolder<>(HttpStatus.NOT_FOUND, WarehouseAPIResponseError
                    .builder()
                    .code("DATA_NOT_FOUND")
                    .message("No records found in the database.")
                    .build());
        }

The method in my controller class;

@GetMapping
    public String getAllWarehouses(Model model) {
        model.addAttribute("listOfWarehouses",warehouseCRUDService.list().getResponseData());
        return "warehouses";
    }

My HTML code;

<div class="container my-2">
    <h1 align="center">Warehouse List</h1>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Code</th>
            <th>Name</th>
            <th>Status</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="warehouse : ${listOfWarehouses}">
            <td th:text="${warehouse.id}"></td>
            <td th:text="${warehouse.code}"></td>
            <td th:text="${warehouse.name}"></td>
            <td th:text="${warehouse.status}"></td>
        </tr>
        </tbody>
    </table>
</div>

I am successfully listing, but if there is an error message I don't know how to display it. I did not use Spring validation methods. Is there any way I can do this in a simple way?

jzProg

You can set the error on backend using model.addAttribute("errorMessage", error)

and then show it in an element if error exists. For example:

<span th:if="${errorMessage != null}" th:text=${errorMessage}/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I create Custom Error messages using JSRuntime?

How to show custom error messages using passport and express

How can I show error messages in change_password in Django

How can I get rsync to show only error messages AND a summary?

How can I get custom HTTP error messages?

How can I throw errors with custom error messages?

How can I show data annotation error messages in .NET Core 2.0? Validation messages are not showing

How can I show the validation messages in vform?

How can I show a custom text in the input, when using ngbDatepicker?

how to show custom modal or error modal using react context with dynamic messages?

How can localized messages in Thymeleaf be processed using SpEL

Spring boot + Thymeleaf custom error messages

how to show custom REST API error messages in javascript client

How to change PHPUnit error messages to show custom errors

How to show error messages in a custom signup template in Django

How can I set custom validation error messages from a controller, in Laravel 8?

How can I change Error messages in python?

How Can I Understand Laravel Error Messages?

How can I suppress these error messages?

Thymeleaf: Can I use messages inside expressions

How can I use a custom tag library with Thymeleaf and Spring Boot?

How do I make error messages show on the webpage?

How can I show notify-send messages triggered by crontab?

How can I show messages depending on selected radio buttons?

How can I show or hide boot messages when Ubuntu starts?

How to show error messages in AngularJS?

how can i display error messages when using the inbuilt reset password and password change in Django

How can I get my error messages to display only if username or password is not entered correctly using jquery?

How do I Set custom error messages on Codeigniter