Sunday, July 24, 2011

JSF - Facelets - How to call onload java script of body tag from ui:composition

When HEAD and BODY tag are used in <ui:composition>. tag, then this wrong usage will create problem .Because of this html DOM is formed with <body> tag inside <body> tag .Because this DOM crashes in Chrome, Opera and Apple safari browser .

What is facelets ui:composition ?

The UI Composition tag is a templating tag that wraps content to be included in another Facelet. Any content outside of the UI Composition tag will be ignored by the Facelets view handler. Any content inside of the UI Composition tag will be included when another Facelets page includes the page containing this UI Composition tag.

Example –

Template.xhtml

 <html>

<head>
<title><ui:insert name="title" /></title>
<body>
<h2><ui:insert name="header" /></h2>
<ui:insert name="message" />
</body>
</html>


Composition.xhtml

 <html>

<head>
<title>Welcome</title>
</head>
<body>
<h2>Hello World!</h2>
How are you today?
</body>
</html


How to call onLoadBody JS script?

As there is no body tag in <ui:composition> then how to call onBodyLoad function after loading content of template ? We can use <div> tag inside those <ui:include> but there is no reliable way to call function on div onLoad or on div unload .Because of this we have to define a template in header of base template and override it to insert onbodyLoad functions .




How to override a facelets?

Example template for override onBodyLoad and onBodyUnload

• Template.xhtml

 <html xmlns="http://www.w3.org/1999/xhtml"

xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h=http://java.sun.com/jsf/html
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>SALPE TECHNOLOGIES</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<ui:insert name="header">
<script language="JavaScript" type="text/JavaScript">
function bodyLoaded(){
alert("Event bodyLoaded called from base template .. ");
}
</script>
</ui:insert>
<ui:insert name="headerUnLoaded">
<script language="JavaScript" type="text/JavaScript">
function bodyUnLoaded(){
alert("Event bodyUnLoaded called from base template .. ");
}
</script>
</ui:insert>
</head>
<body onload="bodyLoaded();" onUnload ="bodyUnLoaded();">
<f:view locale="#{session.locale}">
<table>
<tr>
<td>
<ui:include id="_header" src="HeaderTemplate.xhtml" />
</td>
</tr>
<tr>
<td>
<ui:insert name="_bodyContent">Insert Body Content</ui:insert>
</td>
</tr>
<tr>
<td>
<ui:include id="_footer" src=FooterTemplate.xhtml"></ui:include>
</td>
</tr>
</table>
</body>


GeneralPage.xhtml

 <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<ui:composition
template =”Template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:c="http://java.sun.com/jstl/core" >
<ui:define name="header">
<script type="text/javascript" language="javascript" src=".MyJavascript.js"> </script>
<script type="text/javascript">
function bodyLoaded(){
alert("Event bodyLoaded fucntion from overrriden template");
</script>
</ui:define>
<ui:define name="content">
<f:view locale="#{session.locale}">
<f:loadBundle basename="SALLabel" var="bundle" />
<h:form id="myForm">
<table>
<tr>
<td>
WELCOME TO SALPE Colglomeration !
</td>
</tr>
</table>
</h:form>
</f:view>
</ui:define>
</ui:composition>
</ui:composition>




Standard ways to use facelets.


Do not use <body> tag inside the <ui:composition>. For java script declare source of file before using it .


Tuesday, July 12, 2011

Safari compactibility issue

I got this error while making a JSF page compatible to all browsers including Safari.

Error Details -

This page contains the following errors:

error on line 10 at column 10 : xmlParseEntityRef: no name

Below is a rendering of the page up to the first error.


Solution -

This error comes when speacial characters are used in xhtml for JSF ;

Detailed list

http://www.degraeve.com/reference/specialcharacters.php

More common mistakes



 left single quote           &lsquo;     ‘
right single quote &rsquo; ’
single low-9 quote &sbquo; ‚
left double quote &ldquo; “
right double quote &rdquo; ”
double low-9 quote &bdquo; „
exclamation mark &#33; !
number sign &#35; #
dollar sign &#36; $
percent sign &#37; %
ampersand &#38; &amp; &
Most undectable mistake is that

NOTE

Suppose you have data in data base for example

 &lt;h:outputText styleClass="normal" value="#{testBundle.title}"/>

if this title value from backing bean is like this "Testing & Dev domain"
This value will break XHTML DOM in Safari browser .

Temporary solution is change backing bean /DB value to -"Testing and Dev domain"

Friday, July 8, 2011

Facelets in JSF

JSF is component oriented and event driven web framework to build up web applications.
It is mocking existing technologies like ASP .NET , Apache Tapestry , Java swing etc.
It need some framework to make re-use of XHMTL code and easy way to build pages , composite custom component some what like that of "Apache Tiles" and answer for that is Facelets .
Facelet tag are added in JSF 2.0 as powerful tool to use templates(hence code re-use ) .In jsf 1.2 you have to give facelet handler class but that is not needed in JSF 2.0 .
In JSP we can use page include (static include) and jsp include (dynamic include) for templates .But you have to write all this include tag in all JSPs example header , footer , right , left part of page includes .
As JSF 2.0 has abandoned JSP servlet display technology because it contradicts some phases of JSF life cycle (for more details - improving jsf by dumping jsp ).They have adapted for cleaner appeoch with XHTML and Facelets .
JSP is not true MVC as inexperienced developed may put business logic in java snippets in jsp tags .So there is no imposition of rule .Further more JSP are servlets and they have their phases like translation , compilation and other phases .
JSF facelets with more efficient than JSP because there is no such compilation thing .
With facelets you can create new reusable templates and composite component if you want .

main facelet tags are


I am giving a simple example how to use them -

example template

PageTemplate.xhtml


 <html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<ui:composition>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</meta>
<title>SALPE CORP - INDIA </title>
<LINK rel="shortcut icon" href="icon.ico" />
</head>
<body class="pagebody">
<f:view locale="#{LanguageBean.locale}">
<f:loadBundle basename="Label" var="bundle" />
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" ID="Table2">
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" ID="Table3">
<tr>
<td valign="top">
<h:panelGrid width="100%" id="menuBottomZone1" cellpadding="0" cellspacing="0">
<ui:include id="header" src="/template/HeaderTemplate.xhtml" />
</h:panelGrid>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="13" colspan="3" align="center">
<table width="1003px" border="0" align="center" cellpadding="0" cellspacing="0" ID="Table5">
<tr>
<td align="center">
<ui:insert name="bodyContent">Body Content Area</ui:insert>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<h:graphicImage value="../../newimages/general/spacer.gif" width="5" />
</td>
</tr>
<tr>
<td colspan="3" valign="middle">
<ui:include id="footer" src="/template/FooterTemplate.xhtml"></ui:include>
</td>
</tr>
</table>
</f:view>
</body>
</ui:composition>
</html>


Any other pages - like welcomePage.xhtml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<ui:composition
template="/template/PageTemplate.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<ui:define name="content">
<script language="javascript">
doSomeThingOnLoad();
</script>
<f:view locale="#{LanguageBean.locale}" >
<f:loadBundle basename="CATCmaLabel" var="bundle" />
<h:form id="loginForm">
<table>
<tr>
<td>
Hello JSF - I am showing bodyContent Area :)
Insert here XHTML code you like !
</td>
</tr>
</table>
</h:form>
</f:view>
</ui:define>
</ui:composition>

Look like this when you view
welcomePage.xhtml


















HEADER FOM HEADER TEMPLATE XHTML


Hello JSF - I am showing bodyContent Area :)

Insert here XHTML code you like !




©SALPE CORP FOOTER FOM FOOTER TEMPLATE XHTML