spring - Spring thymeleaf:如何显示警报
问题描述
我正在为一个项目开发一个crud。我想在管理员编辑员工、产品(无论是什么)时显示警报,以显示带有“已成功编辑”消息的警报。我可以显示此消息,但我想在警报中显示它。
我的模型:
// Edit
@GetMapping("/dashboard/contato/editar/{id}")
public String edit(@PathVariable("id") Long id, Model model)
{
Contato contato = contatoService.findById(id);
model.addAttribute("contato", contato);
return "dashboard/pages/contato/editar";
}
@RequestMapping("/dashboard/contato/editar/{id}")
public String update(@Valid Contato contato, BindingResult result, RedirectAttributes attributes,Model model)
{
if(result.hasErrors()) {
attributes.addFlashAttribute("error", "Verifique se os campos
obrigatórios foram preenchidos!");
return "redirect:/dashboard/contato/editar/{id}";
}else{
contatoService.save(contato);
attributes.addFlashAttribute("success", "Editado com sucesso!");
return "redirect:/dashboard/contato";
}
}
解决方案
您可以使用 Jquery 的document ready
功能在每次用户重定向到您的成功页面时显示警报。所以每次页面加载时,浏览器都会自动运行文档就绪功能(更多关于jquery文档就绪功能请参考官方文章-https:
//learn.jquery.com/using-jquery-core/document-ready/
此外,如果您想根据某些动态条件显示警报消息,您也可以使用thymeleaf + javascript 。有关如何在 javascript 中使用 thymeleaf 的更多详细信息,请参阅此官方文档 -
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#javascript-inlining
推荐阅读
- python - 掷骰子的概率
- javascript - 未在反应功能组件中检查空条件
- react-native - 如何在本机反应中在左侧设置一个图标,在标题中心设置文本
- xamarin.forms - Xamarin 表单:图像条目在 android 中未按预期工作
- reactjs - 我们如何使用 webpack 在本地运行 react js 生产代码?
- excel - 通过比较 Excel VBA 中两个不同工作表的 ID 来获取数据时出现问题。给出不正确的数据
- python - send_mail() 不发送电子邮件到关联的电子邮件地址
- xml - XML 模式验证模式限制错误
- wso2is - 带有 WSO2 IS 的 WSO2 APIM - SSO 配置不起作用
- sql - 如何从列表中删除所有重复的对?