gpt4 book ai didi

java - HttpSession 作为 Controller 中的参数

转载 作者:行者123 更新时间:2023-11-30 08:09:07 25 4
gpt4 key购买 nike

我试图了解如何在 spring Controller 中将 HttpSession 作为参数发送。我有一个 jsp,它在单击提交按钮时执行发布请求。在 Controller 中,读取 session 如下

在 Controller 中:

public ModelAndView viewEditFundClass(HttpServletRequest request,HttpServletResponse response,Model model){
HttpSession session = (HttpSession)request.getSession();

java.util.Date startDate = sesseion.getAttribute("startDate");

但是,当我将 Controller 更改为以下内容时,我仍然能够访问 session

 public ModelAndView viewEditFundClass(HttpServletRequest request,HttpServletResponse response, HttpSession session,Model model)

我想知道在Spring中这是如何完成的,即post请求是如何将HttpSession作为参数传递的?这次 session 有效吗?

最佳答案

假设您使用 Spring 3+ @Controller@RequestMapping 处理程序方法,Spring 定义了一组默认的 supported argument types对于您的处理程序

  • Session object (Servlet API): of type HttpSession. An argument of this type enforces the presence of a corresponding session. As a consequence, such an argument is never null.

Spring 使用策略模式来完成此操作,使用接口(interface) HandlerMethodArgumentResolver 。它检查处理程序方法的参数类型,并针对每种类型尝试查找能够解析其参数的 HandlerMethodArgumentResolver。

对于 HttpSession,该实现是 ServletRequestMethodArgumentResolver .

关于java - HttpSession 作为 Controller 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30670715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com