在spring mvc中将HttpSession从一个控制器发送到另一个控制器

时间:2015-07-30 14:10:03

标签: java spring spring-mvc

我正在将我的请求从一个控制器重定向到另一个控制器,但在执行此操作时,我正在丢失会话属性。但我只想使用会话,以便我可以验证用户是否登录。整个条件解释如下:

的Login.jsp 这是用户提供其用户名和密码的页面,请求转到“LoginController”。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Login</title>
</head>
<body>
  ${loginError }
  <h1>Login Details</h1>

  <form action="${pageContext.request.contextPath }/login" method="post">
  <table>
    <tr>
        <td>Username</td>
        <td><input type="text" name="userId" ></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type="password" name="password" ></td>
    </tr>
    <tr>
        <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
</table>
</form>
</body>
</html>

LoginController.java 由于操作是使用post方法“登录”,因此请求映射到“verifyLogin()”方法。

我在调用“CustomerService”类的“loginCustomer()”方法后检查了它的客户名称。

但是当我在会话属性中设置它并将其重定向到与“HomeController”方法匹配的url时,我将失去该会话属性。

@Controller
@RequestMapping(value="/login")
public class LoginController {

  @Autowired
  private CustomerService customerService;

  @RequestMapping(method=RequestMethod.GET)
  public String showLoginForm(){

    return "login";
  }

  @RequestMapping(method=RequestMethod.POST)
  public String verifyLogin(@RequestParam String userId, @RequestParam String password, HttpSession session, Model model){

    Customer customer = customerService.loginCustomer(userId, password);
    if(customer==null){
        model.addAttribute("loginError", "Username or password is not correct");
        return "login";
    }
    session.addAttribute("loggedInUser", customer);
    return "redirect:/";
  }

  @RequestMapping(value="logout", method=RequestMethod.GET)
  public String logoutUser(HttpSession session){

    session.removeAttribute("loggedInUser");
    return "login";
  }

  @ExceptionHandler(Exception.class)
  public ModelAndView handleException(){

    ModelAndView mav = new ModelAndView();
    mav.addObject("loginError", "Usrname or password is not correct");
    mav.setViewName("login");
    return mav;
  }
}

HomeController.java 请求映射到“home()”方法。当应用程序加载时也会调​​用此方法,并且当用户正确登录时,也会调用它。

在这个方法中,我失去了我的会话属性,但我希望它在页面中这个方法映射,即home.jsp这个方法的返回值。

由于此方法中的会话属性不可用,因此它在home.jsp页面中也不可用。 请帮忙。

@Controller
public class HomeController {

  @Autowired
  private ProductService productService;
  @Autowired
  private CategoryService categoryService;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home(Model model) {

    List<Product> products = productService.getFeaturedProducts();
    List<Category> categories = categoryService.getAllCategories();
    model.addAttribute("featuredProduct", products);
    model.addAttribute("categories",categories);

    return "home";
  }

}

3 个答案:

答案 0 :(得分:0)

我建议去RedirectAttributes

redirectAttrs.addFlashAttribute("loggedInUser", customer);

进一步阅读: - Flash Attribute

答案 1 :(得分:0)

我的第一个猜测是您的会话ID cookie可能会在登录和下一页之间丢失。 (可能是因为Web应用程序配置为不使用cookie或cookie设置为错误的域)

验证您在浏览器中检查: 成功登录后,服务器会为当前主机设置会话cookie(chrome或Firefox Dev工具可以帮助您) 此外,您的浏览器会在下一个主页请求中发送相同的cookie。

如果这没有帮助,请在回复中包含设置Cookie和Cookie标题

答案 2 :(得分:0)

我已经使用普通老HttpServletRequest request帮助会话,这似乎符合我的目的。

@RequestMapping(method=RequestMethod.POST)
public String verifyLogin(@RequestParam String userId, @RequestParam String password, HttpServletRequest request, Model model){

   Customer customer = customerService.loginCustomer(userId, password);
   HttpSession session = request.getSession(false);
   if (session == null) {
       session = request.getSession();
   }
   if(customer==null){
       model.addAttribute("loginError", "Username or password is not correct");
       return "login";
   }
   session.addAttribute("loggedInUser", customer);
   return "redirect:/";
}

通过这种方式,您可以通过控制器和jsp页面中的请求对象访问会话值。