jsf - 为什么我不应该将 JSF SessionScoped bean 用于逻辑?

标签 jsf jakarta-ee ejb stateful stateful-session-bean

我正在使用带有购物车样式流程的 JSF 开发一个 Java EE Web 应用程序,因此我想收集多个页面上的用户输入,然后对其进行处理。

我正在考虑为此使用 EJB 3 有状态 session bean,但我的研究使我相信 SFSB 不绑定(bind)到客户端的 http session ,因此我必须通过 httpSession 手动跟踪它,一些附带问题这里 。 . .

1)为什么称为 session bean,据我所知,它与 session 无关,我可以通过将pojo存储在 session 中来实现相同的目的。

2)能够注入(inject)它有什么意义,如果我要注入(inject)的只是这个SFSB的一个新实例,那么我还不如使用pojo?

所以回到我看到的主要问题,JSF 是一种表示技术,所以它不应该用于逻辑,但它似乎是收集用户输入的完美选择。

我可以将 JSF session 范围 bean 设置为我所有请求 bean 的托管属性,这意味着它被注入(inject)到它们中,但与 SFSB 不同的是,JSF 托管 session 范围 bean 绑定(bind)到 http session ,因此始终注入(inject)相同的实例只要 http session 没有失效。

所以我有多层

第一层)处理表示的 JSF 管理的请求范围 bean,每页 1 个。
第 2 层)一个 JSF 管理的 session 范围 bean,其值由请求 bean 在其中设置。
第三层)一个无状态 session EJB,它对 JSF session 范围 bean 中的数据执行逻辑。

为什么这么糟糕?

另一种选择是使用 SFSB,但是我必须将它注入(inject)到我的初始请求 bean 中,然后将其存储在 http session 中并在每个后续请求 bean 中重新获取它 - 看起来很困惑。

或者我可以将所有内容都存储在 session 中,但这并不理想,因为它涉及使用文字键和强制转换。等..等容易出错。 . .和凌乱!

任何想法都值得赞赏,我觉得我正在与这项技术作斗争而不是使用它。

谢谢

最佳答案

Why is it called a session bean, as far as I can see it has nothing to do with a session, I could achieve the same by storing a pojo in a session.



来自旧 J2EE 1.3 tutorial :

What Is a Session Bean?

A session bean represents a single client inside the J2EE server. To access an application that is deployed on the server, the client invokes the session bean's methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server.

As its name suggests, a session bean is similar to an interactive session. A session bean is not shared--it may have just one client, in the same way that an interactive session may have just one user. Like an interactive session, a session bean is not persistent. (That is, its data is not saved to a database.) When the client terminates, its session bean appears to terminate and is no longer associated with the client.



所以它与“ session ”有关。但 session 不一定意味着“HTTP session ”

What's the point of being able to inject it, if all I'm gonna be injecting' is a new instance of this SFSB then I might as well use a pojo?



好吧,首先,您不要在无状态组件中注入(inject) SFSB(注入(inject)另一个 SFSB 就可以了),您必须进行查找。其次,在 HTTP session 和 SFSB 之间进行选择实际上取决于您的应用程序和您的需求。从纯理论的角度来看,HTTP session 应该用于表示逻辑状态(例如,您在多页表单中的位置),而 SFSB 应该用于业务逻辑状态。这在“旧”HttpSession v.s. Stateful session beans 中有很好的解释。 TSS 上的线程也有一个很好的例子,其中 SFSB 有意义:

You may want to use a stateful session bean to track the state of a particular transaction. i.e some one buying a railway ticket.

The web Session tracks the state of where the user is in the html page flow. However, if the user then gained access to the system through a different channel e.g a wap phone, or through a call centre you would still want to know the state of the ticket buying transaction.



但是 SFSB 并不简单,如果您不需要证明使用它们的合理性,我的实用建议是坚持使用 HTTP session (特别是如果所有这些对您来说都是新的)。以防万一,请参阅:
  • Stateless and Stateful Enterprise Java Beans
  • Stateful EJBs in web application?

  • So back to the main issue I see written all over that JSF is a presentation technology, so it should not be used for logic, but it seems the perfect option for collecting user input.



    那不是业务逻辑,那是表示逻辑。

    So I have multiple tiers (...)



    不,您可能有一个客户层、一个表示层、一个业务层、一个数据层。你所描述的看起来更像层(甚至不确定)。看:
  • Can anybody explain these words: Presentation Tier, Business Tier, Integration Tier in java EE with example?
  • Spring, Hibernate, Java EE in the 3 Tier architecture

  • Why is this so bad?



    我不知道,我不知道你在说什么 :) 但你可能应该只是将多页表单信息收集到 SessionScoped bean 中并调用 无国籍进程结束时的 session Bean (SLSB)。

    关于jsf - 为什么我不应该将 JSF SessionScoped bean 用于逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3629417/

    相关文章:

    jsf - <pe :exporter> showcase 中 #{exporterController.customExporter} 的实现在哪里

    java - JSF - 为什么 SessionScoped 类的实例未初始化?

    java - Glassfish ACC(应用程序客户端容器)的文档

    java - 传承理念

    java - 是否可以将 EJB 与具有泛型类型绑定(bind)的类一起使用?

    eclipse - 将 EJB 项目更改为 Web 应用程序

    java - 不使用 xhtml 时填充 IceFaces HtmlSelectOneListbox

    java - JDBC 连接抛出 SocketException

    spring - Tomcat 中未拾取外部化的 message.properties 文件

    ejb - 如何在WebLogic中设置虚拟主机?