Spring MVC怎样实现重定向和转发

重定向

重定向,在要返回的页面视图字符串前面加"redirect:",如"redirect:url"

/** 退出登录*/
@RequestMapping(value = "/logout.action")
public String logout(HttpSession session) {
    /*清除session*/
    session.invalidate();
    /*重定向到登录页面的跳转方法*/
    return "redirect:login.action";
}

转发

转发,在要返回的页面视图字符串前面加"forward:",如"forward:url"

// 通过账号和密码查询用户
User user = userService.findUser(usercode, password);
if (user != null) {
    // 将用户添加到session中
    session.setAttribute("USER_SESSION", user);
    // 转发到主页面
    return "forward:customer/list.action";
}

猜你喜欢

转载自blog.csdn.net/weixin_43894879/article/details/106100347