Tuesday, May 10, 2011

Spring web services using Apache CXF

This blog illustrates how to configure and develop a web service and client using Apache CXF.

Step 1
Download and Install JDK 1.6, Maven and your favorite web application container(tomcat,jboss,glassfish).

Step 2
Create a maven application with archetype "maven-archetype-webapp". Ask Google if you need help running this archetype.

Step 3
Add maven repository for JBoss and dependencies for junit and Apache CXF.

pom.xml
        .
        .
    <repositories>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <properties>
        <cxf.version>2.4.0</cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    </dependencies>
        .
        .

Step 4
Develop the webservice interface and class using JAX-WS annotations.

CalculatorService.java
@WebService
public interface CalculatorService {
    double addNumbers(double a, double b);

    double subtractNumbers(double a, double b);
}

CalculatorServiceImpl.java
@WebService(endpointInterface = "com.ingeniouscamel.springcxfws.service.CalculatorService", 
                        serviceName = "calculatorService")
public class CalculatorServiceImpl implements CalculatorService {
    public double addNumbers(double a, double b) {
        return a + b;
    }

    public double subtractNumbers(double a, double b) {
        return a - b;
    }
}

Step 5
Configure spring context loader and Apache's CXF Servlet in web.xml.

web.xml
       .
       .
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>cxfws-servlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxfws-servlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
       .
       .

Step 6
Configure calculator service endpoint in spring's context by specifying service class and URL.

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <jaxws:endpoint id="calculatorService"
        implementor="com.ingeniouscamel.springcxfws.service.CalculatorServiceImpl"
        address="/calculator"/>
</beans>

Step 7
Run the application in your webapp container and test it using the following URL

http://localhost:<Port>/<WebAppContext>/services/calculator?WSDL



Step 8
Configure Apache CXF's JaxWsProxyFactoryBean class to return a service interface to our webservice and call methods on the web service directly.

CalculatorWebServiceClientTest.java
public class CalculatorWebServiceClientTest {
    private CalculatorService calculatorWebService;

    @Before
    public void setUp() throws Exception {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(CalculatorService.class);
        factory.setAddress("http://localhost:8080/SpringCxfWSApp/services/calculator");
        calculatorWebService = (CalculatorService) factory.create();
    }

    @Test
    public void testAddNumbers() {
        Assert.isTrue(calculatorWebService.addNumbers(3, 5) == 8);
    }

    @Test
    public void testSubtractNumbers() {
        Assert.isTrue(calculatorWebService.subtractNumbers(7, 4) == 3);
    }
}

Download Project
You can download my project from google code. SVN URL is http://ingenious-camel.googlecode.com/svn/trunk/SpringCxfWSApp/

Wednesday, May 4, 2011

Spring web services using JAX-WS

This blog illustrates how to configure and develop a JAX-WS server and client using spring framework.

Step 1
Download and Install JDK 1.6, Maven and your favorite web application container(tomcat,jboss,glassfish).

Step 2
Create a maven application with archetype "maven-archetype-webapp". Ask Google if you need help running this archetype.

Step 3
Add maven repository for JBoss and dependencies for junit, spring, jaxb, and jvnet's jaxws-spring. Since spring is already configured exclude spring from jvnet.

pom.xml
    .
    .
    <repositories>
        <repository>
            <id>jboss</id>
            <name>JBoss Maven Repository</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
           <groupId>com.sun.xml.bind</groupId>
           <artifactId>jaxb-impl</artifactId>
           <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.jvnet.jax-ws-commons.spring</groupId>
            <artifactId>jaxws-spring</artifactId>
            <version>1.8</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    .
    .

Step 4
Develop the webservice class using JAX-WS annotations

CalculatorServiceImpl.java
@WebService
@SOAPBinding(style = Style.RPC, use = Use.LITERAL)
public class CalculatorServiceImpl implements CalculatorService {
    public double addNumbers(double a, double b) {
        return a + b;
    }

    public double subtractNumbers(double a, double b) {
        return a - b;
    }
}

Step 5
Configure spring context loader and jvnet's WSSpringServlet in web.xml.

web.xml
    .
    .
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>jaxws-servlet</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/calculator</url-pattern>
</servlet-mapping>
    .
    .
Step 6   
Configure calculator service as a bean in spring's context. Next, add this service to the web URL that we configured earlier in web.xml.

applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://jax-ws.dev.java.net/spring/core  http://jax-ws.dev.java.net/spring/core.xsd
         http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd">

    <bean id="calculatorService"
        class="com.ingeniouscamel.springjaxws.service.CalculatorServiceImpl" />

    <wss:binding url="/calculator">
        <wss:service>
            <ws:service bean="#calculatorService" />
        </wss:service>
    </wss:binding>
</beans>

Step 7
Run the application in your webapp container and test it using the following URL

http://localhost:<Port>/<WebAppContext>/calculator?WSDL



Step 8
In order to develop a client, first develop a webservice client interface. You can cheat this step by running wsimport command on the WSDL and using the generated source file.

CalculatorServiceImpl.java
@WebService(name = "CalculatorServiceImpl", targetNamespace = "http://service.springjaxws.ingeniouscamel.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface CalculatorServiceImpl {
    @WebMethod
    @WebResult(partName = "return")
    public double addNumbers(
            @WebParam(name = "arg0", partName = "arg0") double arg0,
            @WebParam(name = "arg1", partName = "arg1") double arg1);

    @WebMethod
    @WebResult(partName = "return")
    public double subtractNumbers(
            @WebParam(name = "arg0", partName = "arg0") double arg0,
            @WebParam(name = "arg1", partName = "arg1") double arg1);
}

Step 9
Configure client's spring context to bind the above interface to the web service using spring's JaxWsPortProxyFactoryBean bean (you will find this file in src/main/resources folder of my project).

applicationClientContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="calculatorWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
        <property name="serviceInterface" value="com.ingeniouscamel.springjaxws.client.CalculatorServiceImpl"/>
        <property name="wsdlDocumentUrl" value="http://localhost:8080/SpringJaxWSApp/calculator?WSDL"/>
        <property name="namespaceUri" value="http://service.springjaxws.ingeniouscamel.com/"/>
        <property name="serviceName" value="CalculatorServiceImplService"/>
        <property name="portName" value="CalculatorServiceImplPort"/>
    </bean>    
</beans>

Step 10
You can now load the above client's context (applicationClientContext.xml) and call methods on the web service client directly.

CalculatorWebServiceClientTest.java
public class CalculatorWebServiceClientTest {
    private CalculatorServiceImpl calculatorWebService;

    @Before
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationClientContext.xml");
        calculatorWebService = (CalculatorServiceImpl)context.getBean("calculatorWebService");
    }

    @Test
    public void testAddNumbers() {
        Assert.isTrue(calculatorWebService.addNumbers(3, 5) == 8);
    }

    @Test
    public void testSubtractNumbers() {
        Assert.isTrue(calculatorWebService.subtractNumbers(7, 4) == 3);
    }
}

Download Project
You can download my project from google code. SVN URL is http://ingenious-camel.googlecode.com/svn/trunk/SpringJaxWSApp/

Monday, May 2, 2011

JSF Part 2: Navigation and Ajax

This blog illustrates how to develop page navigation including simple ajax functionality. We shall use the project from JSF Part 1 tutorial.

Step 1
Develop DeptService and DeptBean classes that return list of all departments

DeptService.java
public class DeptService {
    public List<String> getAllDepts() {
        ....
    }
}

DeptBean.java
@ManagedBean
public class DeptBean {
    private DeptService deptService = new DeptService();

    public List<String> getAllDepts() {
        return deptService.getAllDepts();
    }
}

Step 2
Develop EmployeeBean and set up selectedDept variable that will hold the value of selected department name

EmployeeBean.java
@ManagedBean
public class EmployeeBean {
    private String selectedDept;
  
    public String getSelectedDept() {
        return selectedDept;
    }

    public void setSelectedDept(String selectedDept) {
        this.selectedDept = selectedDept;
    }
}

Step 3
Develop index.xhtml that displays list of departments and allows user to select a department

index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
    <title>Departments</title>
</h:head>
<h:body>
    <h:form prependId="false">
        <p:panel header="Search By Dept"> Select Dept :
            <h:selectOneMenu value="#{employeeBean.selectedDept}">
                <f:selectItems value="#{deptBean.allDepts}" />
            </h:selectOneMenu>          
        </p:panel>
    </h:form>
</h:body>
</html>

Step 4
Run the application in your webapp container and test it using the following URL

http://localhost:<Port>/<WebAppContext>/faces/index.xhtml



Step 5
Now that "selectedDept" variable is set, lets invoke a function on EmployeeBean.java to perform search. Add a commandButton after selectOneMenu in index.xhtml that will invoke function "performSearch"

index.html
                .
                .              
            </h:selectOneMenu>         
            <p:commandButton action="#{employeeBean.performSearch}" value="Search" ajax="false"/>         
        </p:panel>
             .
             .

Step 6
Add functionality to EmployeeBean.java and EmployeeService.java that will  search employees based on  selectedDept and cache results in searchResults variable 

EmployeeBean.java
public class EmployeeBean {
    .
    .
    private List<Employee> searchResults;

    public List<Employee> getSearchResults() {
        return searchResults;
    }

    public String performSearch() {
        if (selectedDept.equals("All")  ) {
            searchResults = employeeService.getAllEmployees();
        } else {
            searchResults = employeeService.getEmployeesByDept(selectedDept);
        }
        return "results";
    }
    .
    .
}

EmployeeService.java
public class EmployeeService {
    .
    .
    public List<Employee> getEmployeesByDept(String dept) {
        List<Employee> list = new ArrayList<Employee>();
        for (Employee e : employees) {
            if (e.getDept().equals(dept)) {
                list.add(e);
            }
        }
        return list;
    }
    .
    .
}

Step 7
Configure page navigation in /WEB-INF/faces-config.xml. The xml stanza says "from page index.xhtml, if function employeeBean.performSearch returns "results", then display view results.xhtml

faces-config.xml
<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xi="http://www.w3.org/2001/XInclude" 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">
    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{employeeBean.performSearch}</from-action>
            <from-outcome>results</from-outcome>
            <to-view-id>/results.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

Step 8
Finally, render the list in dataTable

results.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
    <title>Employees</title>
</h:head>
<h:body>
    <h:form prependId="false">
        <p:dataTable value="#{employeeBean.searchResults}" var="emp">
            <f:facet name="header">
                <h:outputText value="Employees search results for " />
                <h:outputText value="#{employeeBean.selectedDept}" />
                <h:outputText value=" Dept" />
            </f:facet>
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Id" />
                </f:facet>
                <h:outputText value="#{emp.id}" />
            </p:column>
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Name" />
                </f:facet>
                <h:outputText value="#{emp.name}" />
            </p:column>
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Dept" />
                </f:facet>
                <h:outputText value="#{emp.dept}" />
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>
</html>

Step 9
Run the application in your webapp container and test it using the following URL

http://localhost:<Port>/<WebAppContext>/faces/index.xhtml


Select a department and hit "Search" button.


Step 10
Instead of two page navigation, all components could be placed in one page and the dataTable could be refreshed using Ajax. This is accomplished by adding update attribute to commandButton which will refresh "empTable" component after setting selectedDept in AjaxEmployeeBean.

ajax.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
    <title>Employees</title>
</h:head>
<h:body>
    <h:form prependId="false">
        <p:panel header="Search By Dept"> Select Country :
            <h:selectOneMenu value="#{ajaxEmployeeBean.selectedDept}">
                <f:selectItems value="#{deptBean.allDepts}" />
            </h:selectOneMenu>

            <p:commandButton action="#{ajaxEmployeeBean.performSearch}" value="Search" update="empTable" />

            <p:dataTable id="empTable" value="#{ajaxEmployeeBean.searchResults}" var="emp">
                <f:facet name="header">
                    <h:outputText value="Employees search results for " />
                    <h:outputText value="#{ajaxEmployeeBean.selectedDept}" />
                    <h:outputText value=" Dept" />
                </f:facet>
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Id" />
                    </f:facet>
                    <h:outputText value="#{emp.id}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Name" />
                    </f:facet>
                    <h:outputText value="#{emp.name}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Dept" />
                    </f:facet>
                    <h:outputText value="#{emp.dept}" />
                </p:column>
            </p:dataTable>
        </p:panel>
    </h:form>
</h:body>
</html>

Step 11
Since this ajax page has no navigation, the performSearch method should return null. Class EmployeeBean could be rewritten as AjaxEmployeeBean, with function performSearch returning null.

AjaxEmployeeBean.java
@ManagedBean
public class AjaxEmployeeBean {
    private EmployeeService employeeService = new EmployeeService();

    private String selectedDept;
    private List<Employee> searchResults;

    public String getSelectedDept() {
        return selectedDept;
    }

    public void setSelectedDept(String selectedDept) {
        this.selectedDept = selectedDept;
    }

    public List<Employee> getSearchResults() {
        return searchResults;
    }    
    
    public String performSearch() {
        if (selectedDept.equals("All")  ) {
            searchResults = employeeService.getAllEmployees();
        } else {
            searchResults = employeeService.getEmployeesByDept(selectedDept);
        }
        return null;
    }
}

Step 12
Run the application in your webapp container and test it using the following URL

http://localhost:<Port>/<WebAppContext>/faces/ajax.xhtml


Download Project
You can download my project from google code. SVN URL is http://ingenious-camel.googlecode.com/svn/trunk/JSF2NavigationAjax/

Sunday, May 1, 2011

JSF Part 1: Primefaces and Maven

This blog illustrates how to configure and develop a JSF application with primefaces. We shall see primeface's dataTable component and dark-hive theme in action.

Step 1
Download and Install JDK 1.6, Maven and your favorite web application container(tomcat,jboss,glassfish)

Step 2
Create a maven application with archetype "maven-archetype-webapp". Ask Google if you need help running this archetype.

Step 3
Add maven repositories for Java Web and Primefaces (see pom.xml preview below)

Step 4
Add maven dependencies for servlet, jsp, jstl, jsf and primefaces (see pom.xml preview below)

pom.xml
   .
   .
<repositories>
    <repository>
        <id>jboss</id>
        <name>JBoss Maven Repository</name>
        <url>https://repository.jboss.org/nexus/content/groups/public/</url>
    </repository>
    <repository>
        <id>primefaces-repo</id>
        <name>Prime Technology Maven Repository</name>
        <url>http://repository.prime.com.tr</url>
        <layout>default</layout>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>dark-hive</artifactId>
        <version>1.0.1</version>
    </dependency>
</dependencies>
   .
   .

Step 5
Add JSF and Primefaces configuration in web application configuration

web.xml
   .
   .
<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>
<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>com.sun.faces.validateXml</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>dark-hive</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping>
   .
   .

  
Step 6
Develop Employee Pojo and Employee Service classes

Employee.java
public class Employee {
    private String id;
    private String name;
    private String dept;

    // add getter and setter methods
}

EmployeeService.java
public class EmployeeService {
    // return list of all employees
    public List<Employee> getAllEmployees() {    
    .
    }
}

Step 7
Develop Employee bean class(with annotation) for our view

EmployeeBean.java
@ManagedBean
public class EmployeeBean {
    private EmployeeService employeeService = new EmployeeService();
    
    public List<Employee> getEmployees() {
        return employeeService.getAllEmployees();
    }
}

Step 8
Develop xhtml page to view all employees in a datatable

results.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
    <title>Employees</title>
</h:head>
<h:body>
    <h:form prependId="false">
        <p:dataTable value="#{employeeBean.employees}" var="emp">
            <f:facet name="header">
                <h:outputText value="Employees" />
            </f:facet>
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Id" />
                </f:facet>
                <h:outputText value="#{emp.id}" />
            </p:column>
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Name" />
                </f:facet>
                <h:outputText value="#{emp.name}" />
            </p:column>
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Dept" />
                </f:facet>
                <h:outputText value="#{emp.dept}" />
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>
</html>

Step 9
Run the application in your webapp container and test it using the following URL

http://localhost:<Port>/<WebAppContext>/faces/results.xhtml



Download Project
You can download my project from google code. SVN URL is http://ingenious-camel.googlecode.com/svn/trunk/JSF1PrimeFacesMaven/