如何使用Thymeleaf显示自定义错误消息?

迈特汉·阿尔通塔斯(Metehan Altuntas)

我正在尝试使用Spring进行CRUD操作。我在前端使用HTML和Thymeleaf。我使用编写的自定义类返回执行的某些操作的结果以及错误消息(如果有)。到目前为止,我没有任何问题。但是,如果在这些操作期间发生错误,并且我通过编写的类返回了此错误,则我不知道如何使用Thymeleaf在HTML上显示该错误。

我将返回此类的对象;

@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;
    }

}

我的错误类别;

@Getter
@Builder
public class WarehouseAPIResponseError {

    private String code;
    private String message;

}

错误示例;

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

我的控制器类中的方法;

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

我的HTML代码;

<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>

我已成功列出,但是如果出现错误消息,我不知道如何显示它。我没有使用Spring验证方法。有什么办法可以简单做到这一点吗?

z

您可以使用以下方法在后端设置错误 model.addAttribute("errorMessage", error)

如果存在错误,则将其显示在元素中。例如:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何更改PHPUnit错误消息以显示自定义错误

如何在Yii中自定义错误显示消息?

如何在DropzoneJS中显示自定义错误消息?

角度2:FormGroup:如何显示自定义错误消息

如何显示API请求的自定义验证错误消息?

如何显示自定义验证错误消息

如何使用自定义错误消息抛出错误?

如何使用护照和快递显示自定义错误消息

如何在Netsuite中使用SuiteScript 2.0显示无堆栈跟踪的自定义错误消息

如何使用jquery验证插件显示自定义验证错误消息?

显示自定义错误消息

如何使用JSRuntime创建自定义错误消息?

如何使用自定义错误消息触发BSoD

Spring Boot + Thymeleaf自定义错误消息

如何自定义Devise错误消息?

如何自定义 PHPMailer 错误消息

使用自定义消息处理错误?

Thymeleaf:如何在JSR-303注释中使用自定义消息密钥

如何显示使用自定义函数的错误路径?

如何使用带有动态消息的React上下文显示自定义模态或错误模态?

如何在角度4中显示来自自定义验证器功能的自定义错误消息?

Joi 验证显示自定义错误消息

在Azure Powershell中显示自定义错误消息并退出

JQuery 验证自定义错误消息未显示

自定义错误验证消息未显示

以wp忍者形式显示自定义错误消息

Node.js + Joi如何显示自定义错误消息?

如何在JSP中显示Spring Security Auth异常的自定义错误消息

如何在javascript客户端中显示自定义REST API错误消息