Why shouldn't I use a JSF SessionScoped bean for logic?

前端 未结 3 1576
天涯浪人
天涯浪人 2021-01-03 03:24

I\'m developing a java EE web app using JSF with a shopping cart style process, so I want to collect user input over a number of pages and then do something with it.

3条回答
  •  旧巷少年郎
    2021-01-03 03:53

    1) 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.

    Correction: an EJB session has nothing to do with a HTTP session. In EJB, roughly said, the client is the servlet container and the server is the EJB container (both running in a web/application server). In HTTP, the client is the webbrowser and the server is the web/application server.

    Does it make more sense now?

    2) 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?

    Use EJB for transactional business tasks. Use a session scoped managed bean to store HTTP session specific data. Neither of both are POJO's by the way. Just Javabeans.

    Why shouldn't I use a JSF SessionScoped bean for logic?

    If you aren't taking benefit of transactional business tasks and the abstraction EJB provides around it, then just doing it in a simple JSF managed bean is indeed not a bad alternative. That's also the normal approach in basic JSF applications. The actions are however usually to be taken place in a request scoped managed bean wherein the session scoped one is been injected as a @ManagedProperty.

    But since you're already using EJB, I'd question if there wasn't a specific reason for using EJB. If that's the business requirement from upper hand, then I'd just stick to it. At least, your session-confusion should now be cleared up.

提交回复
热议问题