- Use integrated Facelets and resource re-location to simplify our facelets
- Replace JSF Extensions Ajax API with new in-built JavaScript APIs to expose Ajax functionality.
- Re-create the app as defined in TOTD #47. This app is built using JSF 1.2 core components and Facelets. It uses JSF Extensions for adding Ajax capabilities. Lets change this app to use newer features of JSF 2.0.
- Edit "faces-config.xml" and change the value of faces-config/@version from "1.2" to "2.0".
- Remove the following fragment from "faces-config.xml":
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
This fragment is no longer required because Facelets is the default view technology in JSF 2.0. But it's important to remember that JSF 2.0 Facelets is disabled by default if "WEB-INF/faces-config.xml" is versioned at 1.2 or older. - Remove the following code fragment from "web.xml":
<init-param>
<param-name>javax.faces.LIFECYCLE_ID</param-name>
<param-value>com.sun.faces.lifecycle.PARTIAL</param-value>
</init-param>
This is only required if JSF Extensions APIs are used. - Edit "welcome.xhtml" and replace code with:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<ui:composition>
<h:head>
<h1><h:outputText value="What city do you like ?" /></h1>
</h:head>
<h:body>
<h:form prependId="false">
<h:panelGrid columns="2">
<h:outputText value="CityName:"/>
<h:inputText value="#{cities.cityName}"
title="CityName"
id="cityName"
required="true"
onkeyup="javax.faces.Ajax.ajaxRequest(this, event, { execute: 'cityName', render: 'city_choices'});"/>
<h:outputText value="CountryName:"/>
<h:inputText value="#{cities.countryName}" title="CountryName" id="countryName" required="true"/>
</h:panelGrid>
<h:commandButton action="#{dbUtil.saveCity}" value="submit"/>
<br/><br/>
<h:outputText id="city_choices" value="#{dbUtil.cityChoices}"></h:outputText>
<br/><br/>
<h:message for="cityName" showSummary="true" showDetail="false" style="color: red"/><br/>
<h:message for="countryName" showSummary="true" showDetail="false" style="color: red"/>
</h:form>
</h:body>
<h:outputScript name="ajax.js" library="javax.faces" target="header"/>
</ui:composition>
</html>
The differences are highlighted in bold and explained below:- "template.xhtml" is no longer required because standard tags are used to identify "head" and "body".
- <h:head> and <h:body> are new tags defined in JSF 2.0. These tags define where the nested resources need to be rendered.
- <h:outputScript> is a new tag defined in JSF 2.0 and allows an external JavaScript file to be referenced. In this case, it is referencing "ajax.js" script and is rendered in "head". The script file itself is bundled in "jsf-api.jar" in "META-INF/resources/javax.faces" directory. It adds Ajax functionality to the application.
- "javax.faces.Ajax.ajaxRequest" function is defined in the JavaScript file "ajax.js". This particular function invocation ensures that "city_choices" is rendered when execute portion of the request lifecycle is executed for "cityName" field. The complete documentation is available in "ajax.js". Read more details about what happens in the background here.
- Refactor "result.xhtml" such that the code looks like as
shown below:
The changes are explained in the previous step, basically a clean Facelet using standard <h:head> and <h:body> tags and everything else remains as is.
Notice how the Facelet is so simplified.
- Facelets are integrated in Mojarra 2.0.
- New tags for resource re-location allow a simpler and cleaner facelet embedded in a JSF application.
- JavaScript APIs provide a clean way to expose Ajax functionality in JSF app.
Have you tried your JSF 1.2 app on Mojarra 2.0 ? Drop a comment on this blog if you have.
File JSF related bugs here using "2.0.0 EDR1" version and ask your questions on webtier@glassfish.dev.java.net.
Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. An archive of all the tips is available here.
转自:http://server.dzone.com/blogs/arungupta/2008/10/15/totd-48-converting-a-jsf-12-ap