PrimeFaces Overlays and Popup Windows
PrimeFaces Overlays and Popup Windows
PrimeFaces: Dialogs
and Other Overlays
Originals of slides and source code for examples: http://www.coreservlets.com/JSF-Tutorial/primefaces/
Also see the JSF 2 tutorial – http://www.coreservlets.com/JSF-Tutorial/jsf2/
and customized JSF2 and PrimeFaces training courses – http://courses.coreservlets.com/jsf-training.html
p:message and
p:messages
• Note
– Main code is same as example in PrimeFaces String Input
10
Elements (Part II).
Bean
@ManagedBean
public class MessageBean {
private String message, emailAddress;
Input Page
<h:form>
<h:panelGrid columns="3" styleClass="formTable">
Message:
<p:editor value="#{messageBean.message}"
required="true" id="message"
requiredMessage="Message cannot be empty"/>
<p:message for="message"/>
Email address:
<p:inputText value="#{messageBean.emailAddress}"
required="true" id="email"
requiredMessage="Email address required"/>
<p:message for="email"/>
</h:panelGrid>
<p:commandButton action="#{messageBean.showMessage}"
value="Send to Customer Support"
ajax="false"/>
12
</h:form>
Alternative Input Page
<h:form>
<h:panelGrid columns="3" styleClass="formTable">
Message:
<p:editor value="#{messageBean.message}"
required="true" id="message"
requiredMessage="Message cannot be empty"/>
<h:message for="message" styleClass="error"/>
Email address:
<p:inputText value="#{messageBean.emailAddress}"
required="true" id="email"
requiredMessage="Email address required"/>
<h:message for="email" styleClass="error"/>
</h:panelGrid>
<p:commandButton action="#{messageBean.showMessage}"
value="Send to Customer Support"
ajax="false"/>
13
</h:form>Same as previous page except that this uses the old-style <h:message for="some-id" styleClass="some-CSS-name"/> instead of <p:message for="some-id"/>
Results Page
...
<p>This is the message that was sent to
customer support:</p>
<hr/>
<div style="font-size: 120%">
<h:outputText value="#{messageBean.message}"
escape="false"/>
</div>
<hr/>
<p>Replies will be sent to
#{messageBean.emailAddress}</p>
...
14
Results (Missing Data)
Messages with p:message or
p:messages automatically adapt to
current PrimeFaces theme.
15
16
© 2015 Marty Hall
p:tooltip
p:tooltip: Overview
• Appearance and behavior
– Theme-aware tooltip that temporarily pops up when the
mouse hovers over a specified element
• Purpose: for giving extra information
– Information that is not important enough to put in text of
page, but that is helpful to user when he or she goes to
type into textfield, click on button, etc.
18
p:tooltip: Summary of Most
Important Attributes
• <p:tooltip for="id" value="text" …/>
– for
• The id of the JSF element that triggers the tooltip. I.e., when
mouse hovers over that element, the tooltip should be displayed.
– value
• The tooltip text. If you want images or other elements in the
tooltip, use <p:tooltip…>content</p:tooltip>.
– showEffect, hideEffect
• jQuery UI animation effects used to show and hide the tooltip.
For details of the options, see the Date Input section.
– showEvent (default: mouseover),
hideEvent (default: mouseout)
• The events that trigger opening or closing the tooltip
19
• Note
– Bean and results page are same as previous example, so
are not repeated here
20
Input Page
<h:form>
<h:panelGrid columns="4" styleClass="formTable">
Message:
<p:editor value="#{messageBean.message}"
required="true" id="message"
requiredMessage="Message cannot be empty"/>
<p:message for="message"/>
<p:tooltip for="message" value="Formatted message for customer service"/>
Email address:
<p:inputText value="#{messageBean.emailAddress}"
required="true" id="email"
requiredMessage="Email address required"/>
<p:message for="email"/>
<p:tooltip for="email" value="The address for replies"/>
</h:panelGrid>
<p:commandButton action="#{messageBean.showMessage}" id="button"
value="Send to Customer Support" ajax="false"/>
<p:tooltip for="button"
value="Expect reply within 24 hours."
showEffect="slide" hideEffect="explode"/>
</h:form>
21
Results
22
© 2015 Marty Hall
p:growl
p:growl: Overview
• Appearance and behavior
– Temporary translucent popup that shows error messages.
Inspired by the Growl notification system for Mac OS.
• Purpose: for displaying error messages
– A replacement for p:messages and h:messages for
displaying FacesMessages
• Messages are a temporary overlay rather than a
permanent part of the page.
24
p:growl: Summary of Most
Important Attributes
• <p:growl …/>
– sticky (true or false [default])
• Should messages stay up? Or fade away after x seconds?
– life (milliseconds [default 6000])
• How long growl stays up before it fades away. Default is 6
seconds (6000 milliseconds)
– autoUpdate (true or false [default])
• Should growl be automatically updated after every Ajax
request? If not, use “id” and target it with the “update”
attribute of p:ajax. See separate Ajax lecture.
– showSummary, showDetail, globalOnly
25
• Same as with p:messages and h:messages
• Note
– Bean and results page are same as previous example, so
are not repeated here
28
Input Page
<h:form>
<h:panelGrid columns="3" styleClass="formTable">
Message:
<p:editor value="#{messageBean.message}"
required="true" id="message"
requiredMessage="Message cannot be empty"/>
<p:tooltip for="message" value="Formatted message for customer service"/>
Email address:
<p:inputText value="#{messageBean.emailAddress}"
required="true" id="email"
requiredMessage="Email address required"/>
<p:tooltip for="email" value="The address for replies"/>
</h:panelGrid>
<p:commandButton action="#{messageBean.showMessage}" id="button"
value="Send to Customer Support" ajax="false"/>
<p:tooltip for="button"
value="Expect reply within 24 hours."/>
<p:growl/>
</h:form>
29
Style Sheet
.formTable {
display: table;
}
.formTable td:first-child {
text-align: right;
}
.error {
color: red;
font-weight: bold;
}
.ui-growl { Style sheet is WebContent/resources/css/styles.css.
Since style sheet is loaded by a JSF tag, it comes after the PrimeFaces
top: 100px; styles, and you do not need to use !important.
30
Results (Missing Data)
31
p:dialog
Dialog Basics
• Give id to dialog with widgetVar
<p:dialog header="Title" widgetVar="dlg">
Arbitrary JSF and HTML content. Hidden at start.
</p:dialog>
• Find dialog in JavaScript: PF('widgetVar')
PF('dlg')
• Open dialog with show()
<p:commandButton … oncomplete="PF('dlg').show()"/>
– Replace oncomplete with onclick if not sending Ajax data.
– Either way, the button must be inside h:form
• Close dialog with hide()
<p:commandButton … onclick="PF('dlg').hide()"/>
– This button might be inside the dialog itself
– You can also omit any explicit button for closing, and user can just
34 click on the “x” at top right of dialog
p:dialog: Summary of Most
Important Attributes
• <p:dialog header="Title" widgetVar="…">
Arbitrary JSF and HTML Content
</p:dialog>
» Note: in general, p:dialog does not have to be inside a form. But, it often contains a
bean property that is updated via Ajax, and in that situation it must be in form.
– header
• Title text. There is also less-used “footer” attribute.
– widgetVar
• ID of the element, for use by buttons or other elements that call
thatId.show() and thatId.hide()
– modal (true or false [default])
• If modal="true", browser screen is grayed out and user cannot
interact with it until dialog is closed
35
38
Supporting Java Code
@ManagedBean
public class DialogBean {
private Date time = new Date();
...
}
39
Example:
Dialog with Animation Effects
<p:commandButton value="Animation"
onclick="PF('dialog4').show()"/>
<p:dialog header="Dialog 4" widgetVar="dialog4"
showEffect="bounce" hideEffect="explode">
<h2>Any type of dialog content here</h2>
<h2>More dialog content here</h2>
</p:dialog>
42
Example: Modal Dialogs
<p:commandButton value="Modal Dialogs"
onclick="PF('dialog5').show()"/>
<p:dialog header="Dialog 5" widgetVar="dialog5"
modal="true">
<h2>Any type of dialog content here</h2>
<h2>More dialog content here</h2>
</p:dialog>
43
Ajax-Based Validation
with Dialog Boxes
48
Bean (Part of MessageBean that
was Shown Earlier)
public String showDialogMessage() {
String messageTemplate =
"<p>Message that was sent to customer support:</p>\n" +
"<hr/>\n" +
"<div style='font-size: 120%%'>\n" +
"%s\n" +
"</div>\n" +
"<hr/>\n" +
"<p>Replies will be sent to %s.</p>";
String messageText =
String.format(messageTemplate, message, emailAddress);
sendMessageToCustomerSupport();
FacesMessage message = new FacesMessage(messageText);
message.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, message);
return(null);
}
49
Input Page
<h:form>
<p:commandButton action="#{messageBean.showDialogMessage}"
value="Send to Customer Support"
process="@form" id="button"
oncomplete="PF('dlg').show()"/>
<p:tooltip for="button"
value="Expect reply within 24 hours."/>
<p:dialog header="Customer Support Message" widgetVar="dlg">
<p:messages autoUpdate="true" escape="false"/>
</p:dialog>
</h:form>
50
Results (Validation Failed)
51
52
© 2015 Marty Hall
p:confirmDialog
p:confirmDialog: Overview
• Appearance and behavior
– Theme-aware dialog box that shows a simple message in
top half and arbitrary content (usually confirm/cancel
buttons) in bottom half.
• Could be implemented with p:dialog, but more convenient
when you want to ask user for confirmation.
• Purpose: to replace JavaScript confirmation
– Used to ask user for agreement
before showing next set of
results or doing next set
of actions. Modal.
54
p:confirmDialog: Summary of
Most Important Attributes
<p:confirmDialog header="Title"
message="Text for Top Section"
widgetVar="...">
Content for bottom of confirmation dialog.
Usually buttons.
</p:confirmDialog>
– header, widgetVar
• Same as with p:dialog.
– message
• Content to go in the top of confirmation dialog (above the buttons).
Alternatively, you can use f:facet inside the confirmDialog body:
<f:facet name="message">
Content for top half of confirmation dialog
</f:facet>
– severity (info or alert [default])
• Determines which icon is shown
55
Typical Usage
<p:confirmDialog … widgetVar="confirmation">
<p:commandButton value="Confirm" …
action="#{bean.confirmAction}"
oncomplete="PF('confirmation').hide(); maybeMoreStuff()"/>
<p:commandButton value="Cancel" …
action="#{bean.cancelAction}"
oncomplete="PF('confirmation').hide(); maybeMoreStuff()"/>
</p:confirmDialog>
56
Example: Input Page
<p:commandButton value="Delete Account"
onclick="PF('confirmation').show()"/>
<p:confirmDialog header="Delete Account"
message="Permanently delete account?"
severity="alert"
widgetVar="confirmation">
<p:commandButton value="No, Save It"
icon="ui-icon-disk"
onclick="PF('confirmation').hide(); PF('saved').show()"/>
<p:commandButton value="Yes, Delete It"
action="#{dialogBean.deleteAccount}"
icon="ui-icon-alert"
oncomplete="PF('confirmation').hide(); PF('deleted').show()"/>
</p:confirmDialog>
<p:dialog header="Account Saved" widgetVar="saved">
<h2>Your account has been saved</h2>
</p:dialog>
<p:dialog header="Account Deleted" widgetVar="deleted">
<h2>Your account has been deleted</h2>
</p:dialog>
57
Example: Results
58
© 2015 Marty Hall
Wrap-Up
Questions?
More info:
http://www.coreservlets.com/JSF-Tutorial/jsf2/ – JSF 2.2 tutorial
http://www.coreservlets.com/JSF-Tutorial/primefaces/ – PrimeFaces tutorial
http://courses.coreservlets.com/jsf-training.html – Customized JSF and PrimeFaces training courses
http://coreservlets.com/ – JSF 2, PrimeFaces, Java 7 or 8, Ajax, jQuery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training