My blog has moved!

You should automatically be redirected in 6 seconds. If not, visit
http://blogs.i2m.dk/allan
and update your bookmarks.

Thursday, 8 May 2008

Using Facelets with NetBeans 6.1 (without nbfaceletssupport)

--
Update 27. May 2008: You can download the Facelets NetBeans plug-in for 6.1 from: http://project-template.googlecode.com/files/nbfaceletssupport-6.1.zip
--

I'm a fan of Facelets when it comes to templating for JSF applications. Prior to version 6.1 of NetBeans I was using the nbfaceletssupport plug-in. Sure, it had its quirks, but overall it was a good plug-in. After installing NetBeans 6.1 and really got into using all the new cool features (shared libraries being the most significant) I realised that the nbfaceletssupport plug-in for NetBeans 6.0 didn't work and a fix had not yet been released at the nbfaceletssupport project site (note: Po-Ting Wu of Sun Microsystems has provided instructions on how to re-compile the plug-in for NetBeans 6.1). This also happened when I switched from NetBeans 5.5.1 to 6.0, so I anticipate that this will also happen in the future. So, I thought to myself, why not use the powerful XML support already built-in NetBeans to give me what I need. What I really needed the plug-in for is code completion. So, here goes. First I decided to drop JSP pages (.jsp) for JSP documents (.jspx). The difference being that .jspx is pure XML and .jsp is HTML.

Facelets file as a JSP page (.xhtml):


<!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">
<head>
<title>JSP facelets file</title>
</head>
<body>

<ui:composition template="/resources/templates/page.xhtml">
<ui:define name="pageTitle">This is a page title</ui:define>
<ui:define name="pageContent">This is the page content</ui:define>
</ui:composition>

</body>
</html>



Facelets file as a JSP document (.jspx):

<jsp:root version="2.0" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page" 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:fn="http://java.sun.com/jsp/jstl/functions" xmlns:t="http://myfaces.apache.org/tomahawk" xmlns:c="http://java.sun.com/jstl/core" xmlns:jsfExt="http://java.sun.com/jsf/extensions/dynafaces">
<ui:composition template="templates/page.xhtml">
<ui:define name="pageTitle">This is a page title</ui:define>
<ui:define name="pageContent">This is the page content</ui:define>
</ui:composition>
</jsp:root>



When creating facelets file as JSP document in NetBeans you will automatically get code completion (without any third-party plug-ins).



For this to work, ensure that the following configuration is set it web.xml:


<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jspx</param-value>
</context-param>



and that the faclets tag library descriptor (TLD) is accessible in the classpath (eg. /WEB-INF/tlds).




References:

6 comments:

Anonymous said...

This is stupid, We have hundreds of *.xhtml files in our project, i will not be changing their extension now.

Allan Lykke Christensen said...

If you don't want to rename your files I recommend that you follow the instructions for re-building the NetBeans plug-in - a 5 min process.

ifnu said...

this is instruction to build your own facelets support for 6.1

http://davidsalter.co.uk/index.php?q=node/104

i'v build it and put it in google code

http://project-template.googlecode.com/files/nbfaceletssupport-6.1.zip

http://ifnu.artivisi.com/?p=8

Allan Lykke Christensen said...

Good job ifnu!

Anonymous said...

hi,
i've downloaded the zip file for updates and also downloaded the latest NB6.1 installed with web features. im getting the following errors don't know why.
Missing required modules for Plugin Facelets Support:
JSP Parser [module org.netbeans.modules.web.jspparser/3 = 200804211638]

thanks sri

Anonymous said...

hi,
i fixed the problem provided here
http://blogs.sun.com/poting/entry/build_and_install_facelets_support

thanks
sri