3

我想在用户提交表单后添加成功通知。

我已经查看了 Pnotify js 库 - https://sciactive.com/pnotify/,它看起来不错。但不知道如何在我的 CRUD 项目中使用。我正在使用 Spring Boot 和 Thymeleaf 作为前端。

我检查了文档:

<button class="btn btn-default source" onclick="new PNotify({
                              title: 'Regular Success',
                              text: 'That thing that you were trying to do worked!',
                              type: 'success',
                              styling: 'bootstrap3'
                          });">Success</button>

这是我的表格:

<form action="#" th:action="@{/contractors-save}"
    th:object="${contractor}"
    method="post" class="form-horizontal form-label-left">

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Name</label>
    <div class="col-md-5 col-sm-5 col-xs-12">
        <input type="text" th:field="*{name}" class="form-control"
                placeholder="name"/>
    </div>
</div>

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Description</label>
    <div class="col-md-5 col-sm-5 col-xs-12">
        <input type="text" th:field="*{description}"
                class="form-control"
                placeholder="description"/>
    </div>
</div>

<div class="modal-footer">
    <button type="button" class="btn btn-default"
            data-dismiss="modal">
        Close
    </button>
    <input type="submit" value="Save" class="btn btn-success">
</div>

通知显示在点击操作上,在我的情况下,我需要在表单提交后显示。你能建议和分享你的专业知识吗?也许还有其他选择。

4

2 回答 2

2

这是一个替代方案。

您可以按照PRG模式并使用RedirectAttributes来添加闪光属性。

例如:

@RequestMapping(value = "/contractor", method = RequestMethod.POST)
public String handle(@Valid @ModelAttribute Contractor contractor,
                     BindingResult result,
                     RedirectAttributes redirectAttributes) {

    // Save contactor ...

    redirectAttributes.addFlashAttribute("message", "Successful!");

    return "redirect:/someGetUrl";
}

只需在处理程序message呈现的视图中显示它/someGetUrl

于 2018-12-09T19:21:55.340 回答
1

正如@Minar Mahmud 建议的那样,我遵循 PRG 模式,将与我的实现分享:

控制器:

@RequestMapping(value = "/contractor-save", method = RequestMethod.POST)
public String saveContractor(@ModelAttribute(value = "contractor") Contractor contractor,
                         RedirectAttributes redirectAttributes) {
    Contractor savedContractor = contractorService.save(contractor);
    redirectAttributes.addFlashAttribute("notification",
        String.format("Contractor \"%s\" successfully saved", savedContractor.getName()));
    redirectAttributes.addFlashAttribute("action", "save");

return "redirect:/contractors";

}

在 HTML 文件中,使用 javascript 显示页面加载通知:

<script th:inline="javascript">
$(document).ready(function () {
    var notification = /*[[${notification}]]*/ "";
    var action = /*[[${action}]]*/ "";
    if (action === 'save') {
        new PNotify({
            title: 'Save contractor',
            text: notification,
            type: 'success',
            styling: 'bootstrap3',
            delay: 3000
        });
    } else if (action === 'remove') {
        new PNotify({
            title: 'Remove contractor',
            text: notification,
            type: 'success',
            styling: 'bootstrap3',
            delay: 3000
        });
    } else if (action === 'update') {
        new PNotify({
            title: 'Edit contractor',
            text: notification,
            type: 'success',
            styling: 'bootstrap3',
            delay: 3000
        });
    }
});

请随意填写评论。我想知道生产可以吗?只是为了确保我没有重新发明轮子。

于 2018-12-10T19:24:18.370 回答