Sunday, May 9, 2010

JSF 1.2 – Eclipse 3.5.2 – JBOSS 5.1

My philosophy has always been "programming should be easy."  I confess that for me it always has been easy.  Picking up new languages isn't hard.  For me concepts flow and paradigms shift without much difficulty.  I use Eclipse as my IDE of choice due to the variety of languages I use.  With Eclipse I can quickly move back and forth between Perl, PHP, Groovy, Ruby, Java, C, SQL and HTML without ever leaving Eclipse.  I love it.

So enter my decision to take up Java Enterprise Web applications.  I've used Java for a number of years now so figured there wouldn't be that big a transition to JEE Web apps.  And while I still hope that is the case, my first attempt at getting a simple app up and running took far longer than it should have. 

First, a little background on the project: I've had this standard project I do in any new language or frame work which constructs a star sector 20 light years across.  Its a great object oriented exercise due to the implicit inheritances and mutation of the star systems, stars, planets, moons and asteroids. 

Since any good project starts with a good database design, I started on the database and Data Transfer Objects incorporating a DAO factory for the pattern.  All that went really well.

Once I had star systems generating properly, it was time to put up a web page to display the information.  It was then I hit molasses.

I settled on a JSF/Richfaces display layer utilizing JBOSS as the JEE container.  The boring details are JSF 1.2, JBOSS 5.1 and Eclipse 3.5.1. 

Rather than give you all the false starts, I'm going to detail what actually worked.  I'll leave the complaining for another blog. 

There really isn't any configuration required for Eclipse or JBOSS so after you've installed JBOSS and Eclipse, bring up Eclipse and switch to the JEE perspective.  Down at the bottom there will be a Servers tab.  Right click New and set up your JBOSS Server.

Next, go to File->New->Dynamic Web Project.  You'll need to give it a name like Eclipse35Jboss51JSF, select JBOSS 5.0 as the target runtime, Dynamic Web Module Version 2.5, and Configuration of JavaServer Faces 1.2.  (see screen below).  Click Finish to create the project.



Now comes the odd part for me.  I've just added the JSF libraries to my build
path but since JBOSS 5.1 has these included you now need to make sure they don't make it into your WAR.  Right click on the project name -> Properties -> Project Facets -> Java Server Faces and toggle the "Include libraries with this application" check box (see below)




Now go back to your project.  You'll be creating two files (index.html and index.jsp).

Right click on the WebContent folder->New->HTML.  For some reason the remains obscure, if you reference the faces file directly it doesn't work.  Its almost as though you need to give JBOSS a little time to compile the servlet. Mare your HTML file look something like this


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Refresh" content= "0; URL=index.faces"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Starting Web App</title>
</head>
<body>
<p>The application will be with you shortly. :-)</p>

</body>
</html>

Basically you just add the redirect, change the title, and put a wee bit of content in the page.


Now create your jsf file named index.jsp.  WebContent->New->JSP


After filling in the file name hit NEXT>
Eclispe does some nice things for (and sometimes to) you.  In this case you can let it fill in the JSF tags for you.  On the next screen you'll get a list of templates.  Select the first one. (see below)


Now you can add some JSF content to your page.

You can add this code snippet between the and 





<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>New Title</title>
</head>
<body>
<f:view>
<h:form id="searchForm">

<h:outputText id="ot1" value="Search Text: "/>
<h:inputText id="it1"/> <br />
<h:commandButton id="cb1" value="Go"/> <br />
<h:commandLink id="cl1" value="About Page"/>

</h:form>
</f:view>
</body>
</html>


Now save everything and deploy it to JBOSS by right clicking on the project name->Run As->Run On Server then selecting JBOSS 5.0 and finish.  If you did everything correctly you should get something like.



Good luck and let me know if it worked.

I'll be posting project updates as the weeks roll on.

2 comments:

edburns said...

Hello Zack's Dad,

Thanks for your nice introductory post. While I certainly understand your loyalty to Eclipse, I have to give a plug for NetBeans 6.8 or 6.9 Beta. The JSF2 support is excellent. Auto completion for managed beans and EL expressions, automatic Facelet template creation, composite component refactoring. Really good stuff. Please check it out if you have time.

Again, thanks for your interest.

Ed Burns, JSF Spec co-lead and author of JavaServer Faces 2.0, the Complete Reference

R K Athey said...

My coworkers who only work in Java love Netbeans and bemoan the fact we use Eclipse at work. Since Eclipse is the standard at our agency I've never taken the time to learn Netbeans. Its on my todo list, however.

We're embarking on a JSF/Richfaces project at work. I'm a database/perl guy by nature and so the steps required for a JEE application seem voluminous. Still, it appears very scalable. The most difficult part is getting over the "You must run your code in a container" concept. I just want it to run dammit.

The thing I find most frustrating about JEE are the class loader issues inside the container. Each container has its own issues and it takes time to decipher what they are. This problem was another fine example of that.