2 Replies Latest reply on Mar 20, 2010 12:49 AM by Pietro M.

    Doubts on the first example of Seam in Action

    Pietro M. Newbie

      Hi,
      I was reading the first Seam example of Seam in Action (the one that encompasses listings 1.1 and 1.2). In this example an injected EntityManager is used to save on the database an instance fo entity GolfTip that is passed to an action method as an argument.


      An excerpt of the example follows:


      @Name("tipAction")
      public class TipAction {
      
           @Logger private Log log;
           @In     private EntityManager entityManager;
              @In     private FacesMessages facesMessages;
          
           @DataModel(scope = ScopeType.PAGE)
           private List<GolfTip> tips;
      
           @DataModelSelection
           @Out(required = false)
           private GolfTip activeTip;
      
           ...
      
           public void add(GolfTip tip)
           {
                log.info("tipAction.add(GolfTip) action called");
                if (tip.getContent().trim().length() == 0) {
                     FacesMessages.instance().add("Please provide a tip from which we may all learn.");
                     return;
                }
      
                log.info("Adding new golf tip...");
                entityManager.persist(tip);
                activeTip = tip;
                facesMessages.add("Thanks for the tip, #{activeTip.author}!");
                retrieveAllTips();
      
                // required to reset form (because the submitted GolfTip is still populated)
                // another option is to outject a null value to the context variable tip
                Contexts.removeFromAllContexts("tip");
           }
      
           ...
      }
      



      As you can imagine, the method is called via EL ( #{tipAction.add(tip)} ) from a JSF page.
      I have two questions about this example:
      1) As far as i know, an Action method (unlike an ActionListener) must not return void. In fact it should return a String that is used to determine the next page to be rendered. In this case how does things work?


      2) The method is built as the EntityManager never fails in persisting the entity instance to the DB. What if the EntityManager fails or encounters an error? Is there a way in that case to catch the error or made a check in order to show a custom Faces Message?


      Thank you to anyone who drops an answer.
      See you. ;)

        • 1. Re: Doubts on the first example of Seam in Action
          Stuart Douglas Master

          1)
          If the action listener returns void then it the app will either follow the appropriate navigation rule defined in pages.xml or alternatively stay on the current page.


          2) If the persist fails then the em is invalid, and needs to be discarded.

          • 2. Re: Doubts on the first example of Seam in Action
            Pietro M. Newbie

            Thank you for your reply.



            Stuart Douglas wrote on Mar 19, 2010 22:31:



            1)
            If the action listener returns void then it the app will either follow the appropriate navigation rule defined in pages.xml or alternatively stay on the current page.


            That method is called from a view component (an UI Command) as an Action, not an ActionListener (as you wrote in your reply). Does this change something?



            2) If the persist fails then the em is invalid, and needs to be discarded.


            Could you please elaborate on that?
            (e.g., What happens in the event of a failure to the flow of execution? Is an exception raised? And last but not least: is there a way in that case to catch the error or made a check in order to show an error Faces Message to the user?)


            Thank you again.