Wednesday, February 17, 2010

JSF 1.2 + PrimeFaces Web Application Tutorial using Eclipse IDE

Setting up a full stack web application with JSF 1.2 Java web framework takes some work. Here I will show you the "mostly manual" way aka non-Ruby on Rails-style.

The project will:
  • Use Eclipse Java EE (aka JDT + WTP)
  • Not use Maven
  • Deployed on Tomcat
  • Use PrimeFaces JSF component library
Why?
I know you're smart enough that this is the hard way. Considering there is AppFuse, Maven archetypes, and SpringSource Tool Suite projects templates to help you start a Java web project the Ruby on Rails way. The reason is because:
  • Educational purposes
  • Make it easy to depend on non-Maven libraries or our own Eclipse projects
Steps
  1. Create new Dynamic Web project in eclipse, select JSF 1.2.
  2. Download JSF-RI (Mojarra) 1.2 either manually or automatically
  3. Make sure to export on Java EE dependencies to get libs on WEB-INF/lib (aka compile vs provided)
  4. Add el-impl dependency and export. This is needed for deploying to Tomcat, not needed for Glassfish other complete Java EE containers.
  5. Download PrimeFaces 1.x and export
  6. PrimeFaces Resources Servlet
  7. PrimeFaces p:resources (JSF 1.x only)
  8. Put f:view on JSP pages (mandatory) -- see forum thread about NullPointerException on p:resources
  9. Beans -> faces-config.xml  . Eclipse IDE helps here, but now you know annotations are much nicer.
Setup Facelets

  1. Download Facelets as dependency and export to WEB-INF/lib
  2. Configure WEB-INF/web.xml

     <!-- Use Documents Saved as *.xhtml -->
     <context-param>
      <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
      <param-value>.xhtml</param-value>
     </context-param>
     <!-- Special Debug Output for Development -->
     <context-param>
      <param-name>facelets.DEVELOPMENT</param-name>
      <param-value>true</param-value>
     </context-param>
     <!-- Optional JSF-RI Parameters to Help Debug -->
     <context-param>
      <param-name>com.sun.faces.validateXml</param-name>
      <param-value>true</param-value>
     </context-param>
     <context-param>
      <param-name>com.sun.faces.verifyObjects</param-name>
      <param-value>true</param-value>
     </context-param>
  3. Configure WEB-INF/faces-config.xml

     <application>
      <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
     </application>
  4. Install JBoss Tools for Facelets editor code completion and other JSF helpers. 
  5. IF you opt for not installing JBoss Tools in the previous step:
    - Configure *.xhtml to be opened with JSP Editor. Open Eclipse Preferences,  go to Window > Preferences > General > Content Types:  Text > JSP > Add (xhtml).
    - Configure Eclipse File Type associations to open *.xhtml as JSP Editor by default, not the Doxia Xhtml editor (which is very slow and not helpful).
  6. Create a sample page.

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Facelets: Number Guess Tutorial</title>
    <style type="text/css">
    <!--
    body {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: small;
    }
    -->
    </style>
    </head>
    
    <body>
    <h1> <ui:insert name="title">Default Title</ui:insert> </h1>
    <p> <ui:insert name="body">Default Body</ui:insert> </p>
    </body>
    
    </html>
  7. Redeploy the web app!

To Do
  1. Add Facelets
  2. Add Spring
  3. Add JPA
  4. Add Spring Transactions
  5. Add Spring Security
  6. Unit Testing

6 comments:

david said...

Hi.

Just one question?.

Can I deploy this project against a tomcat 5.0.x?

Thanks for your post.

Greetings

Hendy Irawan said...

David: It should but with some tweaks..... Probably much easier to get it run on Tomcat 6.x.

Anonymous said...

I just wish that I could find time to visit the campus so that I could see the exhibition; I think that’s an exciting one! www.livegreenhomeshow.com |

www.parhamlawfirmllc.com |

www.researchdogs.com |

www.easternmotorsportsaccessories.com |

www.sportsinfestival.com |

www.gulfshorescomputerrepair.com |

www.trick4internet.com |

www.party-builder.com |

www.countytransportation.com |

www.smokefreevirginiacollege.com |

coach factore outlet said...

Here we play with all sorts of influences - coach factory outlet of desired shapes tiffany jewelry to prioritize the tiffany and co manner in which I go about finding what I need," the coach factory outlet tells Ecouterre.More than 1,000 runners began the race.Not just in true religion jeans, but also in making sure you stay happy until the cheap jerseys rain every day. Hey, $350 is way less than those coach outlet. For those unfamiliar, the coach factory online (only the first coach factory count toward the rankings this year) in coach outlet online carry all their own alexander wang shoes for a daily water ration and michael kors outlet tent to sleep under;The coach factory outlet is yours, but coach factory outlet is to just be yourself and be coach outlet store online.The sleek ensemble came on show through sac burberry. Choose from brands like Bernardo, michael kors outlet and others.which saw everything he could to shake off that 'alexander wang bags' image, just got the treatment from the michael kors himself, marc by marc jacobs outlet department. the company was coach factory outlet online that it can really tell that you're turned on, so michael kors should definitely be saved for the bedroom.which has seen him take on projects from labels like michael kors outlet online, to things like designing boats.

Anonymous said...

This post is one of the most important post that I've ever got to read in my life. The significance of this post on my life is huge.steppescaribbean.co.uk |

hawnbyhotel.co.uk |

partridgeplace.co.uk |

newelfin.co.uk |

subito2.co.uk |

kingsofbassline.co.uk |

beckyandneil.co.uk |

antichew.co.uk |

oldtimegrates.co.uk |

gavinmatthews.co.uk |

Getit said...

superb post. Download Zapya for PC, Zapya free download, Zapya download, Zapya apk to share files and folders

Post a Comment

 
Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan