我已经使用 Struts 2 很长时间了。
如果为我们的操作类实现 SessionAware
接口(interface),我们将获得 SessionMap
但不是 HttpSession
对象。
在 ServletRequestAware
和 ServletResposeAware
的情况下,我们得到 HttpServletRequest
和 HttpServletResponse
对象,但不是像 这样的包装对象>SessionMap
(如果是 SessionAware
)。
我的问题是,如果 Struts 为我们提供了 SessionMap
而不是 HttpSession
来将我们的操作类与 Servlet API 和 Http 协议(protocol)解耦,那么为什么它为我们提供了 HttpServletRequest
和 HttpServletResponse
对象(在 ServletRequestAware
和 ServletResponseAware
的情况下)。
如果 Struts 不想将 Servlet API 和 HTTP 协议(protocol)与操作类解耦,那么为什么它在 SessionAware
接口(interface)的情况下为我们提供 SessionMap
。
为什么我们得不到HttpSession
对象?
最佳答案
In case of ServlectRequestAware and ServletResposeAware we get HttpServletRequest and HttpServletRespose object but not wrapper objects like SessionMap in case of SessionAware.
因为在极少数情况下,它们实际上是必要的(或至少有用),它们会直接公开 servlet 请求和响应。
My question is, if struts is giving us SessionMap instead of HttpSession to decouple our action classes from Servlet API and Http protocol,then why it is giving us HttpServletRequest and HttpServletRespose objects in case ServlectRequestAware and ServletResposeAware.
因为与实际请求或响应相比,您专门需要 HttpSession
的可能性小得多。
If struts don't want to decouple Servlet API and HTTP protocol from the action classes then why it is giving us SessionMap in case of SessionAware interface.
它确实想要解耦Servlet API,这是有充分理由的。它迫使您显式请求 Servlet API 工件,因为它们有代码味道。它不会阻止您获得它们,因为在极少数情况下它们很重要。
HttpSession
几乎只是一个属性映射,它不包含在操作中通常有用的信息。在更罕见的情况下,您仍然可以得到它。
关于servlets - Struts 2 中的 SessionAware 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32401166/