Struts 2中的SessionAware设计 [英] SessionAware design in Struts 2

查看:64
本文介绍了Struts 2中的SessionAware设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Struts 2已有很长时间了.

I have been working with Struts 2 for a long time.

如果为我们的动作类实现 SessionAware 接口,我们将获得 SessionMap ,而不是 HttpSession 对象.

In case of implementing SessionAware interface to our action class we will get SessionMap but not HttpSession object.

如果使用 ServletRequestAware ServletResposeAware ,我们将获得 HttpServletRequest HttpServletResponse 对象,但不会获得诸如的包装对象如果是 SessionAware ,则>> SessionMap .

In case of ServletRequestAware and ServletResposeAware we get HttpServletRequest and HttpServletResponse object but not wrapper objects like SessionMap in case of SessionAware.

我的问题是,如果Struts给了我们 SessionMap 而不是 HttpSession 来使我们的动作类与Servlet API和Http协议脱钩,那么为什么它给了我们> HttpServletRequest HttpServletResponse 对象,以防 ServletRequestAware ServletResponseAware .

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 HttpServletResponse objects in case ServletRequestAware and ServletResponseAware.

如果Struts不想将Servlet API和HTTP协议与操作类分离,那么为什么要在 SessionAware 接口的情况下为我们提供 SessionMap .

If Struts doesn'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.

为什么我们没有得到 HttpSession 对象?

Why we don't get HttpSession object?

推荐答案

在使用ServlectRequestAware和ServletResposeAware的情况下,我们获得HttpServletRequest和HttpServletRespose对象,但在SessionAware的情况下,则没有包装像SessionMap这样的对象.

In case of ServlectRequestAware and ServletResposeAware we get HttpServletRequest and HttpServletRespose object but not wrapper objects like SessionMap in case of SessionAware.

因为那些在实际必要(或至少有用)的极少数情况下直接公开了 servlet 请求和响应.

Because those directly expose the servlet request and response on rare occasions where they're actually necessary (or at least useful).

我的问题是,如果struts提供给我们SessionMap而不是HttpSession来使我们的动作类与Servlet API和Http协议脱钩,那么为什么在提供ServlectRequestAware和ServletResposeAware的情况下,为什么给我们HttpServletRequest和HttpServletRespose对象.

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 的可能性要低得多.

Because it's much less likely you'd specifically need an HttpSession than the actual request or response.

如果struts不想将Servlet API和HTTP协议与动作类分离,那么为什么在使用SessionAware接口的情况下它会为我们提供SessionMap.

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工件.它不会阻止获取它们,因为在极少数情况下它们很重要.

It does want to decouple the Servlet API, for good reasons. It forces you to explicitly ask for Servlet API artifacts because they're a code smell. It doesn't prevent you from getting them because on rare occasions they're important.

HttpSession 几乎只是一个属性映射,它不包含通常在操作中有用的信息.在甚至很少的场合,您仍然可以得到它.

An HttpSession is pretty much just an attribute map, it doesn't contain information generally useful in an action. On the even-rarer occasions you need one you can still get it.

这篇关于Struts 2中的SessionAware设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆