I was triggered by the blog entry of Michael Smith about BootAlert share my code I am using currently in some XPages apps.
Back in august 2014 I wrote already a blog about adding Growl messages to my XPages. There is also described which Bootstrap Growl library I am using.
It was easy to implement, but lets take it a step further.
The Multi Growl PhaseListner will intercept all the message
To accomplish it I added a method to my Utility class, who add a message to the FacesContext
1 2 3 4 5 | public static void addMessage(Severity type, String msg){ FacesContext.getCurrentInstance().addMessage( null , new javax.faces.application.FacesMessage(type, msg, "" )); } |
In my Controller classes, I added one line of code to add the specific message to the FacesContext.
1 | JSFUtil.addMessage(FacesMessage.SEVERITY_INFO, "Configuration has been updated" ); |
The heavy lifting will be done by the phaselistner. Here is the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | package nl.elstarit.phaseListener; import java.util.Iterator; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; import nl.elstarit.utils.JSFUtil; import com.ibm.commons.util.StringUtil; import com.ibm.xsp.util.JSUtil; /* * Enables messages to be rendered on different pages from which they were set. * To produce this behaviour, this class acts as a <code>PhaseListener</code>. * * This is performed by moving the FacesMessage objects: * <li>After each phase where messages may be added, this moves the messages * from the page-scoped FacesContext to the session-scoped session map. * <li>Before messages are rendered, this moves the messages from the * session-scoped session map back to the pag e-scoped FacesContext. * * Only messages that are not associated with a particular component are ever * moved. These are the only messages that can be rendered on a page that is * different from where they originated. * To enable this behaviour, add a * <code>lifecycle</code> block to your faces-config.xml file. That block * should contain a single <code>phase-listener</code> block containing the * fully-qualified classname of this file. * * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a> * * @version This version have the bug corrected: The bug was: * In the same page, the FacesMessage was displayed many times, * with this patch, only different messages is added into new context. */ public class MultiGrowlMessages implements PhaseListener { private static final long serialVersionUID = 3328743500652081238L; /** a name to save messages in the session under */ //private static final String sessionToken = "MULTI_GROWL_MESSAGES_SUPPORT"; /** * Return the identifier of the request processing phase during which this * listener is interested in processing PhaseEvent events. */ public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } /** * Handle a notification that the processing for a particular phase of the * request processing lifecycle is about to begin. */ public void beforePhase(PhaseEvent event) { if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) { } } /** * Handle a notification that the processing for a particular phase has just * been completed. */ public void afterPhase(PhaseEvent event) { if (event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES || event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS || event.getPhaseId() == PhaseId.INVOKE_APPLICATION) { FacesContext facesContext = event.getFacesContext(); handleMessages(facesContext); } } @SuppressWarnings ( "unchecked" ) public static void handleMessages(FacesContext facesContext) { for (Iterator i = facesContext.getMessages(); i.hasNext();) { FacesMessage message = (FacesMessage) i.next(); handleMessage(message); i.remove(); } } public static void handleMessage( final FacesMessage message) { String type = null ; if (FacesMessage.SEVERITY_ERROR.equals(message.getSeverity())) { type = "danger" ; } else if (FacesMessage.SEVERITY_FATAL.equals(message.getSeverity())) { type = "danger" ; } else if (FacesMessage.SEVERITY_INFO.equals(message.getSeverity())) { type = "info" ; } else if (FacesMessage.SEVERITY_WARN.equals(message.getSeverity())) { type = "warning" ; } else { type = "success" ; } createGrowlMessage(message.getSummary(), type); } public static void createGrowlMessage( final String message, final String type) { StringBuilder result = new StringBuilder(); result.append( "$.growl(\n" ); if (StringUtil.isNotEmpty(message)) { JSUtil.addString(result, message); } if (StringUtil.isNotEmpty(type)) { result.append( ",{" ); result.append( "\ttype: " ); JSUtil.addString(result, type); result.append( "}" ); } result.append( ")\n" ); JSFUtil.getViewRoot().postScript(result.toString()); } } |
To use the phaselistner it needs to be registered at the Faces-Config.xml
1 2 3 | < lifecycle > < phase-listener >nl.elstarit.phaseListener.MultiGrowlMessages</ phase-listener > </ lifecycle > |
Happy coding……..
Frank, would you mind either 1) putting this in XSnippets; or 2) attaching a license agreement to this page that indicates the terms for your code (preferable Apache 2)?
That way others will be free to reuse it without having to get copyright permission from you and it can be included in OpenNTF projects.
Thanks!
Nathan, good point about the license. When I wrote the post the idea was already in the back in my head. So I will do it this weekend