Struts and EJB working together. Best practice?

Discussions

EJB design: Struts and EJB working together. Best practice?

  1. Struts and EJB working together. Best practice? (9 messages)

    Hello,

    I'm working on a new J2EE Project and for the first time I want to use EJB and Struts together.
    The two apart is no problem. But to let them working together their doesnt seam to be very good documentation.
    So I would like to hear your opinions on the following way of working.
    Take a little example : we want to use a Shopping Cart that wa store in a statefull session bean and we want to use Struts as web tier with it. For the moment I work as follows.

    In my Strut Actionclass I create a new Shopping Cart bean and put a reference in the sessioncontext :

    public ActionForward execute (...) {
        createShoppingCart(.....);
        HttpSession session = request.getSession();
        session.setAttribute("cart", cart);
    }

    In another Action class I can add some things to the cart

    public ActionForward execute (...) {
        
        HttpSession session = request.getSession();
        ShoppingCart session.getAttribute(cart);
        cart.add(product1);
        cart.add(product2);
    }
        

    But what is now the best way to show the products in another JSP-page? Can I directly access the data form the EJB-bean from the JSP-page, or is this only possible from within an Action class? I will demonstrate both possibilities :

    Option 1)In your JSP-page you use certain tags to manipulate your shopping cart. Something like writing in your jsp-page:

    sessionscope.get(cart.product1)
    sessionscope.get(cart.product2)

    Option 2)No, Option 1 is not possible and the only solution is to extract the data you want from your shopping cart bean in an action class, like this :

     public ActionForward execute (...) {
        
        HttpSession session = request.getSession();
        ShoppingCart session.getAttribute(cart);
        session.setAttribute(cart.product1);
        session.setAttribute(cart.product2);
    }

    And after that you can directly acces your product1 and product2 from the session scope.


    What is the way you people solve this kind of problem? Or is their a totally other and better solution, that I don't know? I'v been looking on the internet for a while and I never found a good and clear explanation for working with Struts and EJB together. The best I found was http://www.devx.com/Java/Article/21065/0/page/1
    But it only tells how to couple Struts with ejb and doesnt go much deeper.

    Thanx in advance!!!!

    Olivier
  2. The example-code I gave in the previous message in this thread is just to demonstrate, without syntax checks etc. It is just to illustrate my thought of working. I remarqued that I for example forgot something in the next method :

    public ActionForward execute (...) {
        
        HttpSession session = request.getSession();
        ShoppingCart session.getAttribute(cart);
        cart.add(product1);
        cart.add(product2);
    }

    It must be at least

    public ActionForward execute (...) {
        
        HttpSession session = request.getSession();
        ShoppingCart session.getAttribute(cart);
        cart.add(product1);
        cart.add(product2);
        session.setAttribute("cart", cart);
    }


    But my question is about a design-issue, not about a detail-implementation issue :-)

    Olivier
  3. Hallo

    I would think you are better of using a single point of entry to you web-tier session and your EJB's.

    Like the following;

    public BaseEJBAction extends Action {

        protected EJBRefence getEJBReference(HttpServletRequest request) {
              return youConcreteEJBReference(request.getSession());
        }


    }

    Where the EJBReference could be defined like the following
    public interface EJBReference {
       public Cart getCart();
       public AnotherEJB getAnotherEJB();

    }

    ----
    This will simplify access to your bean, a more complex solution could be to use a framework like Spring.

    My humble opion!

    Kind regards Per
  4. Hello Per, thanx for your reply. But I don't understand what you want to say exactly.
    Let's take a concrete example :

    Struts => Session Facade => Shopping Cart (statefull session bean)

    I want, in some way, to manipulate my shopping cart in the web tier (using Struts). Let's say we want to add a product to our shopping cart.
    Which methods (and their implementation) should you write in an Action Class, which methods in the session facade and which in de Shopping cart Bean? And on which way will they work together?
    I'm really a bit in trouble with this and you could help me a lot to write a good (may be very simple) example down.

    Thanx in advance!!! Olivier
  5. Hello Per, thanx for your reply. But I don't understand what you want to say exactly.Let's take a concrete example :Struts => Session Facade => Shopping Cart (statefull session bean)I want, in some way, to manipulate my shopping cart in the web tier (using Struts). Let's say we want to add a product to our shopping cart.Which methods (and their implementation) should you write in an Action Class, which methods in the session facade and which in de Shopping cart Bean? And on which way will they work together?I'm really a bit in trouble with this and you could help me a lot to write a good (may be very simple) example down.Thanx in advance!!! Olivier

    Sorry that the "example" wasn't clear enough, If you should solve the problem you specify with the Cart, I would make the following application layers:

    Action -> Delegate -> EJB

    The action should do things like validation of form input, marshal Form to DTO, calling delegate, control application flow.

    Delegate Handling the application model, in the Cart example there will be an add(Product), delete(Product).

    Finally the Ejb will maintain your data persistens.

    An example.

    public void AddCartAction extends Action {

     public ActionForward execute(...) {
        CartDelegate delegate = DelegateFactory.getCartDelegat(request.getSession()); // This could be a singleton ..

      try {

        Product product = new CartHelper().createDTO(form);
        delegate.add(product);


      } catch (ValidationException e) {
       // Here add something to the ActionErrors
      } catch (BusinessException e) {
      // This couldt be thrown if you tell the user that a
      // Businness Exception, example the items in the cart
      // may only have a max weight below 100 kilograms..
      } catch (SystemException e) {
       // shoud forward to an error page, telling the the
       // system is down or something similar.
      }
      return mapping.findForward("success");
     }
    }

    // I have a abstract delegate because its a good place
    // centralize EJB lookup code and general creation.
    public CardDelegate extends AbstractDelegate {
       Session session;
       public CardDelegate(Session session) {
          this.session = session;
       }

       public void add(Product product) throws BusinessException, SystemException {
          // Will create the session bean or fetch it if it already exists
          Cart cart = findOrFetchCart();
          try {
             cart.add(product);
          } catch (RemoteException e) {
              //Maybe log ??
              thrown new SystemException("message",e);
          }
       }
    }


    --------------

    I'll hope this example is clear, and shows the concerns of the layers in the application.
    The delegate (I think) is a very nice layer to have, because we in this layer have the possibility of using not only the EJB but also a stub Cart implementation instead which make the application easier to test.
    All the business method should be in the delegates -> calling the ejb, so you'll might say that the delegate is the 'interface' to the business model.

    Hope this helps..

    /Per
  6. Ok, thanx, this is a very clear example!!!
    Only two little questions left for me :

    1)I use the Session facade pattern as Interface to the business model. Do you think it is also ok as interface to my business model, or is it better to take delegate? (in fact I never used the delegate pattern and don't know very much about it)

    2)you use createDTO(form), I suppose this is to make one object with all the information about the product. Is this to have only one remote call, or is their another reason to use DTO's? I normally don't use them either...


    Thanx again for the very clear example. It was just what I needed!!!!! Olivier
  7. Ok, thanx, this is a very clear example!!!Only two little questions left for me :1)I use the Session facade pattern as Interface to the business model. Do you think it is also ok as interface to my business model, or is it better to take delegate? (in fact I never used the delegate pattern and don't know very much about it)2)you use createDTO(form), I suppose this is to make one object with all the information about the product. Is this to have only one remote call, or is their another reason to use DTO's? I normally don't use them either...Thanx again for the very clear example. It was just what I needed!!!!! Olivier
    ----

    1) Whether you should have a seperate interface for you business delegate, or use the Session facade its a matter of taste. I would prefer to have a Delegate interface (This could then extends the Session interface.
    2) createDTO() (my mistake) should not been taken as litteral, so it had to return some (fuzzy) kind of DTO, but is just to show that you should make some kind of conversion from FormBean to your Domain objects/DTO.. (But use DTO where they give some benefit)

    /Per
  8. Hi,
    I totally agree with Per Joergensen's reply. But it is always good to hide the implementation (business) methods of your bean from client. So it is always a best practice to have a Session Facade layer. Business Delegate is always a good one to have in your design, because it manages the complexity of distributed component lookup and exception handling, and may adapt the business component interface to a simpler interface for use by views.

    Pampa
  9. dear friends,
     so far what I heard was 'strut is for framework and webobject is like webserver'. But my friends told me that we can replace webobject by jboss or strut. Can anybody clear the thing to sk_vimal75 at yahoo dot com. As I am new, can u explain as explain to the beginer?
  10. Hallo

    I do not know what webobject is but the difference between struts and Jboss is simple, because
    Struts is a webframework, which means that it runs on some sort of application server, in this example
    its a webcontainer. Jboss is an application server which primary function is to host EJB components.

    When that is said, your are able to make a webtier with struts and for instance the tomcat webcontainer and
    an applications server running Jboss where your host the application logic.

    Hope this helps.