Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot SpringBoot重定向属性不显示在thymeleaf中_Spring Boot_Thymeleaf - Fatal编程技术网

Spring boot SpringBoot重定向属性不显示在thymeleaf中

Spring boot SpringBoot重定向属性不显示在thymeleaf中,spring-boot,thymeleaf,Spring Boot,Thymeleaf,控制器代码: @控制器 @请求映射(“/admin”) @AllArgsConstructor 公共类管理员控制器{ 私有AdminUserService AdminUserService; @后映射(“/login”) 公共字符串登录(@RequestParam String username、@RequestParam String password、@RequestParam String kaptcha、重定向属性、HttpSession会话){ 字符串errorMsg; if(Stri

控制器代码:

@控制器
@请求映射(“/admin”)
@AllArgsConstructor
公共类管理员控制器{
私有AdminUserService AdminUserService;
@后映射(“/login”)
公共字符串登录(@RequestParam String username、@RequestParam String password、@RequestParam String kaptcha、重定向属性、HttpSession会话){
字符串errorMsg;
if(StringUtils.isEmpty(kaptcha)){
errorMsg=“kaptcha不能为空”;
addFlashAttribute(“errorMsg”,errorMsg);
返回“重定向:管理员/登录”;
}
if(StringUtils.isEmpty(用户名)| | StringUtils.isEmpty(密码)){
errorMsg=“用户名或密码不能为空”;
addFlashAttribute(“errorMsg”,errorMsg);
返回“重定向:管理员/登录”;
}
字符串代码=(字符串)session.getAttribute(Const.kapchaCode);
if(StringUtils.isEmpty(code)| |!kaptcha.equals(code)){
errorMsg=“无效的卡普查代码”;
addFlashAttribute(“errorMsg”,errorMsg);
返回“重定向:管理员/登录”;
}
AdminUser login=adminUserService.login(用户名、密码);
if(login==null){
errorMsg=“无效的用户名密码组合”;
addFlashAttribute(“errorMsg”,errorMsg);
返回“重定向:管理员/登录”;
}
session.setAttribute(“loginUser”,login.getAlias());
setAttribute(“loginUserId”,login.getAdminUserId());
返回“重定向:/admin/index”;
}
模板:


当出现错误时,errorMsg不会显示。
我已经检查了errorMsg是否确实进入了RedirectAttributes,但它不会显示在页面上;

请在redirected controller
admin/login
中设置该值

@RequestMapping(value = "admin/login", method = RequestMethod.GET)
public String OtherController(@ModelAttribute("errorMsg") String errorMsg, Model model) {
    model.addAttribute("errorMsg", errorMsg);
    return "login";//template name
}

仍然不工作。更改为
attributes.addAttribute(“errorMsg”,errorMsg)后
@Shawn99感谢您的回复,我正在学习Spring并误解了这个问题。尽管如此,我已经更新了我的答案,请检查它是否有效。我假设您有一些控制器用于重定向
管理/登录
,如果您有控制器签出更新的答案,如果没有,则需要使用
模型#addAttribute
和只需使用
return“login”而不是
return“redirect:admin/login”