Skip to main content

Towards Java EE Nirvana

Posted by cayhorstmann on February 2, 2009 at 6:27 PM EST

Java EE 6 makes it pretty straightforward to crunch out a basic web + database application. This semester, my software engineering class is building web apps, and I decided it is simpler to have them use JSF + JPA in Glassfish v3 rather than some technology that seems easy at first and then lets them down.

Here is a laundry list of recent simplifications.

  • Author JSF pages as facelets: XHTML + JSF tags. No icky JSP syntax
  • Use annotations: @ManagedBean @Stateless @Entity @EJB @PersistenceContext. No icky XML configuration
  • Use EZNavigation™ in JSF. Actions directly return the view ID string. Hooray—no navigation in faces-config.xml
  • No more busywork interfaces for session beans
  • Use entity annotations, and let JPA generate the tables. No SQL!
  • Package everything in a single WAR

The WAR file has the structure

web pages (.xhtml)
WEB-INF/
  web.xml
  faces-config.xml
  classes/
    managed beans
    stateless session beans
    entity beans
    META-INF/
      persistence.xml

There are three cookie-cutter XML files that you never need to touch. In particular, faces-config.xml is empty! The rest is XHTML and Java.

Unfortunately, setting up your tools is a bit of trouble because everything is right now a moving target. Here are the steps.

  1. Install Glassfish v3 Prelude
  2. Add the glassfish-v3-prelude-version/glassfish/bin directory to your PATH
  3. Run glassfish-v3-prelude-version/updatetool/updatetool. Add the Glassfish EJB container. Install any available updates.
  4. Check the version of the JSF implementation in the updatetool. If it is < 2.0, delete it and go back to the list of available modules. You should get a 2.0 module that you can install. You need version 2.0.0.9 or above.
  5. Start the server:
    asadmin start-database
    asadmin start-domain
  6. Download and unzip this test application. Edit build.properties and set the path of your Glassfish installation. Run ant. Point your browser to http://localhost:8080/SimpleQuiz

  7. You can use the EE version of Netbeans 6.5 out of the box. With the EE version of Eclipse 3.4, install the Eclipse Glassfish adapter . When you import the sample project into Eclipse, make a “Dynamic web project”, set the source path to src/java (not src), the web path to web (not WebContent), and uncheck “Generate deployment descriptor” . Both Netbeans and Eclipse do a beautiful job with hotswapping.

Overall, I was very happy. This is a huge step forward from five years ago, when Bruce Tate refused to eat Elephant again.

Have we reached Nirvana? Not quite. Here are some remaining hassles:

  • I hate writing (and reading) all those getters and setters. Did I ever mention that Java needs support for properties?
  • For a simple app, it is a nuisance to separate managed beans and session beans. Web beans, erm, Java Contexts and Dependency Injection, should solve that, but I didn't have the courage to throw that into the mix. Next semester :-)
  • Error reporting has become better, in no small part thanks to facelets. But I still get the stack trace from hell more often than I'd like.
Related Topics >>

Comments

The package name has changed after I posted the blog. Change javax.faces.model to javax.faces.bean for importing the annotations.

Hi, I just downloaded your example and trying to build the project. Before I did, I change the build.properties to point to the glassfish.dir to glassfishv3_prelude/glassfish directory. When I build, I got the following errors. Buildfile: build.xml prepare: [mkdir] Created dir: /home/cchen/project/glassfish/SimpleQuiz/build [mkdir] Created dir: /home/cchen/project/glassfish/SimpleQuiz/build/WEB-INF/classes [mkdir] Created dir: /home/cchen/project/glassfish/SimpleQuiz/dist compile: [javac] Compiling 4 source files to /home/cchen/project/glassfish/SimpleQuiz/build/WEB-INF/classes [javac] /home/cchen/project/glassfish/SimpleQuiz/src/java/com/horstmann/simplequiz/managed/QuizMB.java:8: cannot find symbol [javac] symbol : class ManagedBean [javac] location: package javax.faces.model [javac] import javax.faces.model.ManagedBean; [javac] ^ [javac] /home/cchen/project/glassfish/SimpleQuiz/src/java/com/horstmann/simplequiz/managed/QuizMB.java:9: cannot find symbol [javac] symbol : class SessionScoped [javac] location: package javax.faces.model [javac] import javax.faces.model.SessionScoped; [javac] ^ [javac] /home/cchen/project/glassfish/SimpleQuiz/src/java/com/horstmann/simplequiz/managed/QuizMB.java:15: cannot find symbol [javac] symbol: class ManagedBean [javac] @ManagedBean(name = "quiz") [javac] ^ [javac] /home/cchen/project/glassfish/SimpleQuiz/src/java/com/horstmann/simplequiz/managed/QuizMB.java:16: cannot find symbol [javac] symbol: class SessionScoped [javac] @SessionScoped [javac] ^ [javac] 4 errors BUILD FAILED It looks like the build script expecting a JSF jar file. But I thought it is already included in the glassfish packages. Here is the output from ./pkg list with glassfish-jsf 1.2.12-1 installed u--- included. NAME (AUTHORITY) VERSION STATE UFIX felix 1.2.2-0 installed ---- glassfish-amx 3.0-28.5 installed ---- glassfish-api 3.0-28.5 installed ---- glassfish-common 3.0-28.5 installed ---- glassfish-ejb 3.0-28.5 installed ---- glassfish-grizzly 1.8.6.2-0 installed ---- glassfish-gui 3.0-28.5 installed ---- glassfish-hk2 3.0-28.5 installed ---- glassfish-jca 3.0-28.5 installed ---- glassfish-jdbc 3.0-28.5 installed ---- glassfish-jdbc-gui 3.0-28.5 installed ---- glassfish-jdbc-management 3.0-28.5 installed ---- glassfish-jpa 3.0-28.5 installed ---- glassfish-jsf 1.2.12-1 installed u--- glassfish-jta 3.0-28.5 installed ---- glassfish-management 3.0-28.5 installed ---- glassfish-nucleus 3.0-28.5 installed ---- glassfish-registration 3.0-28.5 installed ---- glassfish-scripting 3.0-28.5 installed ---- glassfish-web 3.0-28.5 installed ---- glassfish-web-gui 3.0-28.5 installed ---- glassfish-web-management 3.0-28.5 installed ---- javadb 10.2.2.1-0 installed u--- pkg 1.0.7-15.1269 installed ---- pkg-java 1.0.7-15.1269 installed ---- python2.4-minimal 2.4.4.0-15.1269 installed ---- did I missed something ?

@arinte: Did JSF 1.x fulfill all the hopes we had for it? Sadly, it didn't. But time moves on, and JSF 2.0 looks a lot better. JSF has one big idea--let someone else do the heavy lifting of writing powerful components, and little old me drops them onto a page. The trouble with "big idea" development is that the 1.x version often disappoints. But what's the alternative? The "little idea" approach ("Oh neat, I get CRUD screens with 3 mouse clicks") doesn't really get you very far either. I admire the JSF and EJB folks for doggedly simplifying and fixing their stuff, without losing sight of the big idea.

Sorry Cay, "but rather than some technology that seems easy at first and then lets them down" == JSF

Are you still using the prelude version? You should use preview. I don't know if that still has 1.2, but it has 2.0. Currently 2.0.0-14.

Hello, I've been unsuccessful in getting point 4)"Installing the JSF 2.0 module from the update center". I deleted the 1.2 version using the updatetool, but I don't see the 2.x version. Can anyone assist? What could I be doing wrong or possibly be missing? I just see Glassfish JSF version 1.2.10-1 from the "Available Add-ons".