1 Reply Latest reply on Aug 19, 2010 1:03 PM by lvdberg

    Application error into h:messages

    chicco0386

      Hi all,
      my question is: Can I save the application error into FacesMessages and after display it into h:messages without use pages.xml and so the redirect attribute?.


      I explain me better:
      when an error accours I want to stay in the current page and display the error into the same page inside and h:messages tag.
      I'm example if there is a database error during the list action, I want operate like above.


      I genererate my project from a table DB instance.


      CAN YOU HELP ME PLEASE?


      VERY THANKS

        • 1. Re: Application error into h:messages
          lvdberg

          Hi,


          You can do that vut it means you have to capture you error before Seam does. Meaning you must do that in the bean where the error is produced. However you must reRender your page partially to visualise the error.


          I am doing this with the help of an observer-bean which gets triggered from an error event, the bean receives the error an produces the trigger for a ajax-push  which in  turn rerenders a grwwl, which is a nice tag from the Primefaces library. All a bit complex, but it works.


          Leo



          @Name("incidentMessagePushBean")
          @Scope(ScopeType.APPLICATION)
          public class IncidentMessagePushBean {
          
               @In(required=false) FacesMessages facesMessages;
               @In(required=false) protected User currentUser;
               @Logger Log log;
               
               protected HashMap<String,PushEventListener> listeners = new HashMap<String, PushEventListener>();
          
               protected HashMap<String, LinkedList<FacesMessage>> operatorMessages = new HashMap<String, LinkedList<FacesMessage>>();
               
               @In(required=false) private Map<String, String> messages;
               
               public void addListener(EventListener l) {
                    synchronized(this){
                         listeners.put(currentUser.getUserName(), (PushEventListener) l);
                    }
                    log.info("Added a listener for user " + currentUser.getUserName() + " we now have " + listeners.size() + " listeners." );
               }
          
               /** This method catsches all the defined events and sends an update 
                * request to all the views. Its minimum delay time is 5 seconds to ensure that
                * the display stays relatively stable.
                */
               @Observer(value={"es.esam.im4u.incidentmessage.new", "es.esam.im4u.operator.login"} )
               public synchronized void processEvent(String o, String h, String d){
                    
                    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, messages.get(h), messages.get(d));
                    
                    /** Check if this message if directed to all users at-once */
          
                    if (o != null && o.equalsIgnoreCase("all")){
                         Set<String> keys = operatorMessages.keySet();
                         for (String name: keys){
                              if (operatorMessages.get(name).size() < 8) operatorMessages.get(name).add(msg);
                              else {
                                   LinkedList<FacesMessage> list = operatorMessages.get(name);
                                   if(!list.isEmpty())
                                        list.removeFirst();
                                   operatorMessages.get(name).add(msg);
                              }
                         }
                    } else {
                         if (operatorMessages.get(o) == null ){
                              LinkedList<FacesMessage> list = new LinkedList<FacesMessage>();
                              list.add(msg);
                              operatorMessages.put(o, list);
                         } else {
          
                              if (operatorMessages.get(o).size() < 8) operatorMessages.get(o).add(msg);
                                   else{
                                        LinkedList<FacesMessage> list = operatorMessages.get(o);
                                        if(!list.isEmpty())
                                             list.removeFirst();
                                        operatorMessages.get(o).add(msg);
                                   }
                              
          
                         }
                    }
                    
                    
               }
               
               /** Provides a value which will be used to give a message to the current user */
               public void growl(){
                    if (currentUser != null && operatorMessages.get(currentUser.getUserName()) != null && operatorMessages.get(currentUser.getUserName()).size() > 0 ){
                         FacesContext context = FacesContext.getCurrentInstance();
                         for (FacesMessage m: operatorMessages.get(currentUser.getUserName())) {
                              m.setSummary(messages.get(m.getSummary()));
                              m.setDetail(messages.get(m.getDetail()));
                              context.addMessage("test", m);
                         }
                         operatorMessages.get(currentUser.getUserName()).clear();
                    }
               }
               
          }