Friday, January 7, 2011

Setup Eclipse JEE6 Primefaces and Glassfish - Part I

This is the first of a 2 part series on setting up a web application using eclipse, glassfish, JEE6 and Primefaces.  The second  article covers testing with Arquillian and JPA configuration.

My current employer is using Glassfish application server with Grails development framework.  I am really impressed with the terse nature of Grails, maybe more so with Groovy.  Unfortunately with Grails GSP there tends to be a fair amount of javascript in the pages for AJAX.  My fear is that the javascript ends up containing some business logic or sequencing/coordination rules for the use of the business rules.  I've used JSF in the past to remove much of the javascript issues and JSF makes a great framework for AJAX style web applications.  Knowing that JEE6 comes with JSF/Facelets as standard view technology, CDI for dependency injection and JSR330 for validation, it seems like a good transition away from Grails and to JSF.

I am going to build on a series of steps from Nik Carlson on JBoss blogs but want to tailor it more to Glassfish and running from eclipse.  Nik's posts are at http://in.relation.to/Bloggers/AHitchhikersGuideToJavaEE6ApplicationSetupPartI.  Check them out, they are awesome!

Let's start with a download of eclipse helios IDE for JEE Developers from http://www.eclipse.org/downloads/.
Download the zip (32-bit even if tou are on 64bit OS for JBoss tools XUL runner incompatibility with 64-bit).  Unpack the zip to c:\ so you'll end up with c:\eclispe.  Also install a 32 bit JDK to c:\, my current is C:\jdk1.6.0_23.  Modify the c:\eclipse\eclipse.ini and add the line <code>-vm
C:\jdk1.6.0_23\bin\javaw.exe</code> per http://wiki.eclipse.org/Eclipse.ini.  This will run eclipse under a jdk instead of a jre which will help out maven.

Let's install maven while we're at it.  Download release 2.2.1 from http://maven.apache.org/download.html and unpack it to c:\.  Now you should have C:\apache-maven-2.2.1.  Setup local environment variable for MAVEN-HOME and point it to C:\apache-maven-2.2.1.   Add a path entry for %MAVEN_HOME%\bin.  Verify maven setup by running mvn -v from a command prompt.  You should get output like:

Apache Maven 2.2.1 (r801777; 2009-08-06 15:16:01-0400)
Java version: 1.6.0_23
Java home: C:\jdk1.6.0_23\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"

While we're at it, let's install Glassfish v3 from http://dlc.sun.com.edgesuite.net/glassfish/3.1/promoted/.  Scroll to the bottom of the list and select the "latest" installer (windows is  latest-glassfish-windows.exe ). Run the installer and follow the directions.

Now let's start eclipse and install some plugins....
  3) Glassfish integration via Servers tab, add new server, Download additional server adapters, Oracle Glassfish Server Tools.


Now we can finally start our project.  From eclipse, select new maven project.  Select the create simple project to skip the archetype selection.  Enter com.examples for the groupid on the next page, enter primetest for the artifact, select war packaging, and enter Primefaces JSF for the name and description.  Then click finish.

Edit the project properties and add CDI in the CDI Settings.  Also, convert the project to faceted form and change the java version to 1.6 and add Dynamic Web Module version 3.0.  Update the configuration (addition configuration available at the bottom of the dialog) and change the webapp directory to the maven standard src/main/webapp and click the create web.xml.  Now select the runtime tab and select your glassfish server.  Click ok.  Then right click the project, maven, update project configuration.

Now let's add a couple of files that will get JSF and CDI up and running.  Add two files to your web-inf (faces-config.xml and beans.xml).  

faces-config.xml:
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0"/>

beans.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" />

Lastly, let's add a JSF page.  Create greetings.xhtml in the webapp directory:


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:ui="http://java.sun.com/jsf/facelets">
 <h:head>
  <title>
   Greetings
  </title>
 </h:head>

 <h:body>
  <h:outputText value="Hello world"/>
 </h:body>
</html>

Now target the the project to glassfish and start up the server.  Then goto http://localhost:8080/primetest/greetings.jsf and you should get the classic hello world.  While that's really special, let's spruce it up some by adding a backing bean.

Create a com.examples.greeting package in the src/java folder.  Add a GreetingBean and add this code:


package com.examples.greeting;

import javax.enterprise.inject.Model;

@Model
public class GreetingBean 
{
 public String getGreeting()
 {
  return "Hello world";
 }
}

Now edit the greetings.xhtml and change the static Hello World to use the bean.  Remove the Hello World text and enter #{ and hit ctrl-space.  Now it looks up available beans!  Sweet.  Select the greetingBean and hit .  Now the available methods are shown!  Select greeting.  Save the file and notice the glassfish console redeploys automatically.  Now refresh the page (http://localhost:8080/primetest/greetings.jsf) and see Hello World again.  It's a little tough to tell if it worked, so let's change the bean to return "Hello World from bean". Save the bean and refresh the browser.  Cool!

Let's add a little primefaces button on the page just to show we can.  Start by adding primefaces dependency.  Open the pom and and:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>prime-repo</id>
<name>Prime Technology Maven Repository</name>
<url>http://repository.prime.com.tr</url>
<layout>default</layout>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.RC2</version>
</dependency>
</dependencies>

Save the file and select run as/maven package.  This will build the snapshot war file in the project's target directory.  Go find the primefaces.jar in the web-inf/lib and extract the tlds from it's META-INF and put into your web-inf.  We'll now add primefaces tags to the jboss pallete.  First, stop your glassfish server and remove your project from it.  Then open the greetings.xhtml and add jsf capabilities via the dialog.  Then use the pallette import to add primefaces i and primefaces p tag libraries.  

Now let's add a form to our page.  You can use the pallete to select f:form or enter it by hand.  Then select the primefaces p:button and add it to the form.  Set it's id to myButton and value to   Now the page should look like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Greetings</title>
</h:head>

<h:body>
<h:form>
<p:button id="myButton" value="My Button">
</p:button>
<h:outputText value="#{greetingBean.greeting}" />
</h:form>
</h:body>
</html>

Let's add the project back to the Glassfish server and start  it up.  Refresh the browser and see the new button.  Nice.

A few troubleshooting issues:
JSF Capabilities will try to install when you open the first xhtml.  If the app is already deployed the install fails. It's best to undeploy, open an xhml, install jsf capabilities, then redeploy the web app.

Without sonatype m2 extras, the maven depenendencies (primefaces in this case) won't be deployed to glassfish.

The jdk settings in eclipse kept giving me fits (claiming annotations required jdk 1.5 while eclipse was reporting 1.6).  Install the jdk compiler plugin in the pom fixed that issue.

Here's a zipped project based on what we've covered so far. primetest.zip

In the next installment we'll hook up arquillian for testing and add some more server functionality.

Also see:




Sunday, January 2, 2011

Grails and Primefaces

I spent the first day of the new year learning about Grails Plugins and how to integrate JSF2, Webflow and Primefaces together in a grails application.  My goal was to create a plugin that I could use to add what I think would be a great set of features for UI development altogether into a Grails app.  No luck so far but I did learn some things.

First, Spring's bigotry against JSF shines thru with Grails.  Aside from ICEFaces donating a plugin to try and get you to use IceFaces in Grails, there is not much information about JSF integration out there.

Second, WebFlow seems to be a second class citizen in Grails these days too.  It used to be a default plugin, now they say you just add it yourself, and the source code says it's on github, but there's no evidence of it there as near as I can tell.

Those two issues put a halt on my progress but there are a few things I learned that may be of interest to you.  First, plugins write to the web.xml via the XXXGrailsPlugin.groovy.  For example,
def doWithWebDescriptor = { webXml ->
def contextParam = webXml."context-param"

contextParam[contextParam.size()-1]+{
'context-param' {
'param-name'('javax.faces.DEFAULT_SUFFIX')
'param-value'('.xhtml')
}
}

can be used to add the JSF default suffix context parm.  There are similar methods for writing filters, servlets, listeners etc.  Check out XMLSlurper (http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html).

The svn repository of existing plugins is at https://svn.codehaus.org/grails-plugins and is a good resource to see how others have gone about things.

The spirng examples svn now includes a Primefaces/WebFlow example project (https://src.springframework.org/svn/spring-samples/webflow-primefaces-showcase).  It's a good starting point, particularly for non-Grails.

This new year I hope to increase my internal fortitude and take a run at JSF/Grails/Primefaces plugin again!

Friday, November 19, 2010

Agile Computing

With the current industry emphasis on Agile methodology over the last decade, I figured it's about time I chimed in...... but first a little about me.

I started working my first computer programming assignment while a student at Ohio State University in 1983.  The project was for a PhD student in BioMedical Technologies.  I was assigned to help him with technical aspects of his dissertation.  There was no project manager, no methodology -- just me and Dr. Chattergee (my customer).  We worked feverishly to get a set of MRI readings of the human brain to display on the CAD/CAM equipment at OSU using a device independent graphics interface.  The first version of the software simply allowed you to flip thru the cross-section images of the brain that were taken at 3mm intervals.  A few weeks later, version 2 stacked up all the images and skinned them to display a 3d model of the brain from the same MRI images.  We worked further to add selection of deformities with gradient edge detection and splining algorithms to model the selected deformity.  Later we added the ability to detect the same deformity thru all the 3mm cross-sections and model that in 3D also.

Little did I know, that was my first Agile experience.  Given the 12 principals of Agile manifesto, we met 10 of them:
1 - Customer Satisfaction is Priority One
2 - Welcome Changing Requirements
3 - Deliver Frequently
4 - Business and Technical People Should Work Together
5 - Have motivated Individuals, give them tools to do the job and get out of the way.
6 - Face to Face interactions are most effective communication
7 - Working Software is most effective measure of progress
8 - Work pace must be sustainable for long periods of time
9 - Continuous Attention to technical excellence
10 -Simplicity to maximize the amount of work done is essential.
11 - The best architectures, requirements, and designs
emerge from self-organizing teams.
12 - Reflect on the work and processes at regular intervals.

I bet you've guessed which 2 principals were not met, but here's a few hints.  Without customer satisfaction, I was unemployed and couldn't pay my student loans and I seldom miss a meal.  I think that covers 1, 2, 5 and 7.  Since I worked directly with the customer and business person, frequently face to face where we delivered/demonstrated application features, that covers 3, 4 and 6.  I came up with the architecture and  since I'm pretty simple, so was the architecture (9, 10 and 11).  So the 2 we missed were reasonable pace and reflection (8 and 12). 

More on Agile next time, how it has progressed over the years, and how I am using it today.