Skip to main content

TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application - Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3

Posted by arungupta on August 14, 2009 at 8:41 AM PDT



href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
#93
showed how to get started with href="http://jcp.org/en/jsr/detail?id=316">Java EE 6
using href="http://www.netbeans.org/servlets/NewsItemView?newsItemID=1413">NetBeans
6.8 M1 and
GlassFish v3 by
building a simple Servlet 3.0 + JPA 2.0 web
application. JPA 2.0 + Eclipselink was used for the database
connectivity
and Servlet 3.0 was used for displaying the results to the user. The
sample demonstrated how the two technologies can be mixed to create a
simple web application. But Servlets are meant for server-side
processing rather than displaying the results to end user. href="http://jcp.org/en/jsr/detail?id=314">JavaServer
Faces 2 (another new specification in Java EE 6) is designed
to fulfill
that purpose.



This Tip
Of style="font-weight: bold;">The style="font-weight: bold;">Day (TOTD) shows how
to enhance the application created in TOTD #93 and use JSF 2 for
displaying the results.

  1. Right-click on the project, select "Properties", select
    "Frameworks", click on "Add ..." as shown below:



    href="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-add-framework.png"> style="border: 2px solid ; width: 700px; height: 382px;" alt=""
    src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-add-framework.png">



    Select "JavaServer Faces" and click on "OK". The following
    configuration screen is shown:



    href="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-framework-added.png"> style="border: 2px solid ; width: 636px; height: 539px;" alt=""
    src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-framework-added.png">



    Click on "OK" to complete the dialog. This generates a whole bunch of
    files (7 to be accurate) in your project. Most of these files are
    leftover from previous version of NetBeans and will be cleaned up. For
    example, "faces-config.xml" is now optional and "forwardToJSF.jsp" is
    redundant.
  2. Anyway, lets add a POJO class that will be our style="font-style: italic;">managed bean.
    Right-click on "server" package and select "New", "Java Class ...",
    give the name as "StateList". Change the class such that it looks like:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    package server;



    import java.util.List;

    import javax.faces.bean.ManagedBean;

    import javax.persistence.EntityManagerFactory;

    import javax.persistence.PersistenceUnit;

    import states.States;



    /**

     * @author arungupta

     */

    @ManagedBean

    public class StateList {

        @PersistenceUnit

        EntityManagerFactory emf;



        public List<States>
    getStates() {

           
    return
    emf.createEntityManager().createNamedQuery("States.findAll").getResultList();

        }

    }



    Here are the main characterisitcs of this class:

    1. This is a POJO class with @ManagedBean annotation. This
      annotation makes this class a managed
      bean
      that can be used in the JSF pages. As no other
      annotations or parameters are specified, this is a request-scoped
      managed bean with the name "stateList" and lazily initialized. More
      details about this annotation are available in the href="https://javaserverfaces.dev.java.net/nonav/docs/2.0/managed-bean-javadocs/index.html">javadocs.
    2. The persistence unit created in href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
      #93 is injected using @PersistenceUnit annotation.
    3. The POJO has one getter method that queries the database
      and return the list of all the states.
  3. In the generated file "template-client.xhtml", change the
    "head" template to:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    Show States



    and "body" template to:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
                   
    <h:dataTable var="state" value="#{stateList.states}"
    border="1">

                       
    <h:column><h:outputText
    value="#{state.abbrev}"/></h:column>

                       
    <h:column><h:outputText
    value="#{state.name}"/></h:column>

                   
    </h:dataTable>



  4. This uses the standard JSF "dataTable", "column", and "outputText" tags
    and uses the value
    expression
    to fetch the values from the style="font-style: italic;">managed bean.


If the application is already running from  href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
#93, then Deploy-on-Save
would have automatically deployed the entire application. Otherwise
right-click on the project and select Run (default shortcut "F6"). The
results can be seen at
"http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and looks like:



alt=""
src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-output.png">



The updated directory structure looks like:



alt=""
src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-dir-structure.png">



There were multiple files added by the JSF framework support in
NetBeans. But as I said earlier, they will be cleaned up before the
final release.



Also refer to other href="http://blogs.sun.com/arungupta/tags/javaee6">Java EE 6
blog entries.



Please leave suggestions on other TOTD that
you'd like to see.
A complete archive of all the tips is available href="http://blogs.sun.com/arungupta/tags/totd">here.




Technorati: totd
glassfish
v3 href="http://technorati.com/tags/mysql">mysql href="http://technorati.com/tags/javaee6">javaee6 href="http://technorati.com/tags/javaserverfaces">javaserverfaces
jpa2 href="http://technorati.com/tags/netbeans">netbeans




Related Topics >>

Comments