从控制器显示错误或成功

时间:2018-08-19 14:58:48

标签: spring spring-mvc sweetalert2

我正在使用sweetAlert来显示popUp消息,我想知道是否有任何原因从控制器中显示此警报

myPage.html

<form th:object="${ecran}"  th:action="@{/createIssue}"  method="post"  >
 ...

<a class="btn btn-outline-danger" type="submit" >Valider la demande</a>
</form>

我的提醒

swal("Are you sure you want to do this?", {
buttons: ["Oh noez!", true],
});

myController.java

@PostMapping("/createIssue")
public String creerUneDemande(@Valid @ModelAttribute("ecran") Ecran ecran, BindingResult result,
        RedirectAttributes redirectAttributes) {
...

}

1 个答案:

答案 0 :(得分:1)

您可以通过使用模式属性在控制器中设置一个标记并基于标记值在视图页面中显示警报来实现相同的目的。

@PostMapping("/createIssue")
public String creerUneDemande(@Valid @ModelAttribute("ecran") Ecran ecran, BindingResult result,
        RedirectAttributes redirectAttributes) {

...
//if everything working fine then set the flag value
redirectAttributes.addFlashAttribute("flag","showAlert");
}

在视图页面中,在javascript代码中接受flag的值,并执行类似以下的条件。

if('${flag}' == 'showAlert'){
 swal("Are you sure you want to do this?", {
 buttons: ["Oh noez!", true],
 });
}