这篇文章将为大家详细讲解有关Springboot使用Map实现将错误提示输出到页面的方法 ,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
主要思路:在controller层我们将错误信息put进map中,然后通过视图解析器跳转到目标页面,在目标页面中在通过指定标签内的th:text将错误消息取出。
例:
1.编写controller代码
@PostMapping("/user/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, Mapmap ){ if (!StringUtils.isEmpty(username) && "123456".equals(password)){ return "dashboard"; }else { map.put("msg","用户名或密码错误"); return "login"; } }