JSF2 Composite Components 4
JSF2 Composite Components 4
optionN: valueN});
Warnings
Dont forget the curly braces
Misspelled option names are ignored no error message
No trailing comma after the last option
23
Main Options for
datepicker({})
defaultDate (default: today)
Initially selected date
changeMonth, changeYear (default: false)
Should jQuery include a dropdown list to let you choose
the month or year?
dayNames (default SundaySaturday)
monthNames (default JanuaryDecember)
Names to use for the days and months. For other
languages. There are also various options for short
versions of days.
numberOfMonths (default: 1)
How many months to show at a time
24
Component File:
Interface Section
<composite:interface>
<composite:attribute name="value" type="java.util.Date"/>
<composite:attribute name="changeMonth" default="false"/>
<composite:attribute name="changeYear" default="false"/>
<composite:attribute name="numberOfMonths" default="1"/>
</composite:interface>
25
Component File:
Implementation Section
<composite:implementation>
<h:outputStylesheet name="jquery-ui.css" library="css/jquery-ui"/>
<h:outputScript name="jquery.js" library="scripts" target="head"/>
<h:outputScript name="jquery-ui.js" library="scripts" target="head"/>
<h:outputScript name="jquery-ui-setup.js" library="scripts" target="head"/>
<h:inputText value="#{cc.attrs.value}" id="date">
<f:convertDateTime pattern="MM/dd/yyyy"/>
</h:inputText>
<script type="text/javascript">
$(function() {
var fieldId = coreservlets.jQueryIdString("#{cc.clientId}" + ":date");
$(fieldId).datepicker({ changeMonth: #{cc.attrs.changeMonth},
changeYear: #{cc.attrs.changeYear},
numberOfMonths: #{cc.attrs.numberOfMonths}});
});
</script>
</composite:implementation>
26
Custom JavaScript File
var coreservlets = {};
coreservlets.escapeColons = function(string) {
return(string.replace(/:/g, "\\:"));
};
coreservlets.jQueryIdString = function(fieldId) {
return("#" + coreservlets.escapeColons(fieldId));
};
27
This sets up an empty object to use as a namespace,
to reduce the chance of name conflicts. The functions
will be properties in that object. This is similar syntax
to static methods in Java. See the coreservlets.com
JavaScript tutorial for details on this technique.
Given "foo:bar:baz", returns "foo\:bar\:baz". For use
in inputDate5, where each input element has its own
id, but the id will contain ":" due to the JSF
conventions. In jQuery, if you have <... id="foo:bar"/>,
you cannot do $("#foo:bar") because ":" has special
meaning. So, you must do $("#foo\:bar") instead.
Given "foo:date", returns "#foo\:date". That is, prepends "#" on the
front, then escapes colons. This yields an escaped version of the
CSS id selector string that can be used by jQuery.
Example Usage:
Reservations at the JSF Resort
Idea
Same as previous: let user make a hotel reservation.
But, pass options to the datepicker function
Managed Bean
Unchanged from previous example
Page that uses component
Same as last example: collects name, checkindate, and
checkout date.
But, supplies attributes that customize the number of months
shown and whether a month or year menu is shown
Results page
Unchanged from previous example
28
Facelets Page that Uses
Component
...
<h:form id="registrationForm">
<h2>Register for the JSF Resort</h2>
<utils:inputName2 value="#{resortBean2}"
firstNamePrompt="First (Given) Name"
lastNamePrompt="Last (Family) Name"/>
<b>Start date:</b>
<utils:inputDate5 value="#{resortBean2.startDate}"/><br/>
<b>End date:</b>
<utils:inputDate5 value="#{resortBean2.endDate}"
changeMonth="true" changeYear="true"
numberOfMonths="2" id="checkoutDate"/>
<h:message for="registrationForm:checkoutDate"
styleClass="error"/><br/>
<h:commandButton action="#{resortBean2.register}"
value="Register"/><p/>
</h:form>
... 29
Result: Page that Uses
Component
30
Default date picker.
Date picker with number of months
displayed set to 2, and month/year
menus enabled.
Result: Results Page
31
2013 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/
J ava, J SF 2, PrimeFaces, Servlets, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Wrap-Up
Summary
Basics
Load J avaScript via h:outputScript with target="head"
Passing arguments to JavaScript
Find real (HTML) id with
"#{cc.clientId}" +":local-id"
Replace colons with "\\:"
Pass result to jQuery $ function
Caution
Consider using existing component libraries (e.g.,
PrimeFaces, RichFaces) before wrapping your own
components
33
2013 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/
J ava, J SF 2, PrimeFaces, Servlets, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Questions?
JSF2, PrimeFaces, Java7, Ajax, jQuery, Hadoop, RESTful WebServices, Android, Spring, Hibernate, Servlets, JSP, GWT, andother JavaEEtraining.
AlsoseeJSF2tutorial andPrimeFaces tutorial.