您当前的位置: 首页 > 

梁云亮

暂无认证

  • 3浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

redirect重定向向前台传递数据

梁云亮 发布时间:2020-07-29 11:37:03 ,浏览量:3

SpringBoot中重定向时,采用Model向前台传递数据时会失败,正确的做法是使用RedirectAttributes。

示例:

@RestController
@RequestMapping("/admin")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 打开后台管理员登录页面
     * @return
     */
    @GetMapping("/showLogin")
    public String showLogin() {
        return "admin/login";
    }

    @PostMapping("/login/{username}/{password}")
    public String lgoin(@PathVariable("username") String username, @PathVariable("password")String password,
                        HttpSession session, RedirectAttributes attributes){
        User user = userService.login(username, password);
        if(user != null){
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        } else {
            attributes.addFlashAttribute("msg","用户名或密码错误");
            return "redirect:/admin/showLogin"; //重定向
        }
    }
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0384s