Skip to main content

Using an IDE to write a JSF 2.0 App

Posted by driscoll on May 18, 2009 at 9:40 AM PDT

So, you're on the cutting edge, writing JSF 2.0 applications, but you just get tired of jkl; editing in vi... You can still do JSF 2.0 applications using your IDE, but the price is that you won't have some of the on the fly error checking (especially in your Facelets page).


First, a little about the status of various support efforts - unfortunately, tools support will lag for JSF 2.0, just as it has in other major releases.


I've checked on three different IDEs -

  • IntelliJ
  • IntelliJ looks to be doing some very interesting work on JSF 2.0 support, which is unfortunately not publicly available yet - it'll be in Maia, the codename for their next release. You can use the current rev of IntelliJ with JSF 2.0, but you'll get a death of a thousand red squiggles. The good news is that although Maia's release date isn't announced, it appears likely that some sort of early access will be available by JavaOne - but I don't know anything secret, I'm just reading their forums and blogs, so take that prediction for what it is - a guess. I'll blog more once they've made a public release.

  • NetBeans
  • NetBeans 6.7 won't have EE 6 support, unfortunately, but they do seem to be integrating what had been an unsupported Facelets plugin into their main line of code - Facelets support is available in their update center as a download. However, this is for Facelets support as a JSF 1.2 addon - it gives an error if you try to use it with JSF 2.0. So, it seems like we'll have to wait for NetBeans.next to really have good JSF 2.0 support - but you can use it now, since NetBeans relies on the libraries bundled in your app server for compilation - you just won't have the GUI sugar that you might want when developing your Facelets XHTML page. See below.

  • Eclipse
  • Facelets support was dropped from the Galileo release of Eclipse - it was bumped until the next release, which should happen at around the same time that the rest of Eclipse gets EE 6 support. This puts Eclipse at about the same place as NetBeans, leaving you with something that you can use to edit and compile, but missing code completion and any meaningful debugging support.


I'll run through using both Eclipse and NetBeans with JSF 2.0, but first, a word about what you'll get when you're done - you'll have a nice GUI editor that handles the JSF 2.0 Java classes just fine, but will treat the XHTML as simple XHTML, without knowing any of the Facelets content. So, it'll tell you if you've mismatched a tag, but if you misspell, for instance, h:commandButton as h:commandbuttn, it's up to the server to tell you that when you view the page. Better support will be forthcoming, as I mentioned above, but for now you'll have to be patient.


Also, I'm hardly an expert at either platform, so for those of you who are, comments and suggestions are always welcome.

Instructions for Using Eclipse with JSF 2.0

First, go download a version of Eclipse that bundles GlassFish, or some other server that supports JSF 2.0.


From that page:



The product distributions contain Eclipse IDE 3.4.1 Java EE IDE, preconfigured GlassFish Server v2.1 and GlassFish Server v3 Prelude and optionally a J2SE 6 1.6.0_12. The Mac OS installer is not available with JDK.


Install and launch it.


Alternatively, you can use your existing Eclipse 3.4 Ganymede installation and get the GlassFish plugin for Eclipse. Read these instructions to find out how.


To create the project:


File ... New ... Dynamic Web Project


Opens a Wizard.


Project Name: JSF 2.0 Test


Target Runtime: push "New" button, choose Glassfish V3 PROMOTED

Make sure that the "Create a new local Server" box is checked, and click "Next" (these instructions assume you'll be installing a new version of GlassFish. If you already have a version with JSF 2.0, you should be able to figure out which parts are different easily enough.)


Choose your VM (on my Mac, it was the default 1.6)

Enter the name of an empty directory where you want to install the promoted build of GlassFish v3. Click "Install Server". Accept the license at the pop-up, and click "Finish". The server will then install.

Click "Next".


The next page, you should be able to just accept all the defaults. Click "Finish". You've now configured your Target Runtime to be something that supports JSF 2.0.


Since the rest of the values that you need are all the defaults, you can go ahead and click "Finish" again". In particular, you don't need to select the "JSF" runtime configuration - since you won't really be using the faces-config.xml file, anyway - since for most uses, it isn't necessary for JSF 2.0.


Your Project is now set up.


You should know what to do next - create a JSF 2.0 app. You can delete the index.jsp page, and fill in with a bunch of xhtml files... The Eclipse IDE knows about the new JSF 2.0 APIs, since by default it uses the API jars bundled into the server. Here's an example of a project file structure that I did that worked, just so you can see what files you'll have to change.

Instructions for Using Netbeans with JSF 2.0

You'll need to download the latest NetBeans Nightly Build (no, Beta won't do, since that has a few showstopper bugs for our use).


You'll also need the latest release of GlassFish v3 nightly. (Remember, you're cutting edge, right?) If you're just going after JSF 2.0, though, you could always just use GlassFish v3 Prelude updated to the latest Mojarra release, as described in the release notes.


If you're using GlassFish v3, then you'll need to add the plugin to make that work.


Once all that's done, it's easy - just create a standard JSF project, using the v3 server that you downloaded, just like any other JSF project that you create. You'll need to delete the faces-config.xml file, and change the web.xml file a bit, but like in Eclipse, the server acts as the repository for the requisite APIs. I've attached a sample file to the blog so you can see an example to work from.

Related Topics >>

Comments

ntruchsess: Thanks for the tip - you can also do that in Netbeans as well. And that will help with some of the issues. But unfortunately that still won't fully do the job, since at least the composite component feature isn't amenable to that solution. As I said, if it was really just that easy, they'd have done it already.

jim + frank: All one has to do to enable xsd-support in a reasonable good xml-editor is specifying a valid xsi:schemaLocation-attribute for each namespace being used. Without xsi:schemaLocation an xml-editor will only support schema that are known to the editor. In eclipse it's possible to extend the set of well-known schema in xml-preferences so you don't have to specify xsi:schemaLocation after installing the xsds into eclipse.

kvillaca: Yes, you'll only see 1.2. that's expected, and I should have mentioned that. frank: Since xsds exist for the new features, it apparently requires more than that - but since I don't write IDE plugins, I couldn't say what.

Very good post :) Thanks

Referring to your example of a misspelled JSF tag: Maybe I have missed something, but isn't it that the only thing an IDEs XML/XHTML editor would need to appropriately support syntax checking and code completion is a set of XML schema definitions covering the JSF standard tags (including facelet tags providing templating) and any 3rd party component libraries in use? Testwise I am maintaining a XSD file declaring my own custom JSF tags and their attributes, because I don't expect my IDE to understand, which java files are going to be packed into a component library and what the JSF annotations mean. Theoretically we should be able to generate an XSD automatically from the annotations, but the effort to create an appropriate schema is very small compared to the many usages of the schema (this applies all the more to the JSF/Facelet tags).

Hi, I did all those steps, although after the Eclipse with the GlassFish plugin have finished the download, and I have clicked finish and finish again. I can't see any options to create projects for JSF2. When I have created one Dynamic Web Projetc and have deleted the index.jsp I just can't create any file with .xhtml extension simple because the IDE does not recognized as a view file. I have the GlassFish Promoted into server place. when I went check the properties from my project it is showing JSF 1.1 and even clicking into down row, I just can see the 1.2 What I did wrong ? Thanks

yup, grizzl

Great summary! I also blogged about JSF 1.x and Eclipse at: http://blogs.sun.com/arungupta/entry/totd_54_java_server_faces

For some reason, I haven't been able to use Glassfish v3 with the latest Mojarra builds. But Eclipse works fine with Tomcat and Mojarra. (1) Add the jsf-api.jar and jsf-impl.jar to your project (2) In Properties -> Java Module Dependencies, select these JARs. That way, they get added to the WEB-INF/lib directory.

JBossTools http://www.jboss.org/tools/ are basically a set of Eclipse plugins. They are supporting Facelets 1,1,x