Working with JSF and JPA has been edited by Phani Balaji Madgula (Jun 02, 2008).

(View changes)

Content:

The Java Persistence API is a new programming model under EJB3.0 specification (JSR220) for the management of persistence and object/relational mapping with Java EE and Java SE. With JPA, developers can easily develop java applications that perform operations on relational databases using java objects and mapping. In that way, java applications developed using JPA are not only portable across different platforms, but also applications can be easily developed using simple yet powerful programming model provided by JPA. This greatly improves application maintainability against ever changing database world. JPA insulates applications from all the complexity and non-portable boilerplate code involved in database connectivity and operations.

Apache geronimo uses OpenJPA for providing Java Persistence API to Java EE applications deployed in the server. Even though JPA is a part of EJB3.0 spec, it is independent of it. Hence, JPA can be used in JavaSE, web and ejb applications in the same uniform way.

Below tutorial explains developing a JSF (JavaServerFaces) application using JPA for persistence. The application has an ejb application and a web application. The ejb application uses JPA to manipulate entities in the database. The web application uses JSF to connect to ejbs to trigger operations on the entities. The database being used is the embedded derby database shipped with geronimo. The web application displays a list of currencies of various countries along with the corresponding USD conversion rate. The web interface allows user to add a new currency, edit the existing currency and delete a currency etc.

The application can be downloaded from this [link|^JPA-JSFApp.ear].

In order to develop, deploy and run the application, the following environment is required.

  • Sun JDK 5.0+ (J2SE 1.5)
  • Eclipse 3.3.1.1 (Eclipse Classic package of Europa distribution), which is platform specific
  • Web Tools Platform (WTP) 2.0.1
  • Data Tools Platform (DTP) 1.5.1
  • Eclipse Modeling Framework (EMF) 2.3.1
  • Graphical Editing Framework (GEF) 3.3.1
  • Apache Geronimo 2.1

The tutorial covers following topics.

  • Setting the Eclipse environment
  • Creating ejb application with entities
  • Creating web application with JSF configuration
  • Deploying the (ear) application
  • Setting up the database tables
  • Running the application

Setting the Eclipse environment

1. Download the geronimo v2.1 and install it in your system. Look into the geronimo documentation for
instructions

2 Install the eclipse IDE and download geronimo eclipse plugin and install it on top of eclipse. Look into the
geronimo eclipse plugin documentation for instructions.

3. Create a runtime environment for geronimo v2.1 in the eclipse. Look into the geronimo eclipse plugin
documentation for instructions to install a runtime for geronimo.

Creating ejb application with entities

1. Open the eclipse IDE and change the perspective to Java EE by clicking on Windows => Open Perspective => Other. It will open up Open Perspective wizard. Select Java EE from the list and click OK button.

Unable to render embedded object: File (1.jpg) not found.

2. Right click on the Package Explorer and select EJB Project.

Unable to render embedded object: File (2.jpg) not found.

3. This will open up the New EJB Project wizard. Provide the values for Project Name, Target Runtime as given in the figure below.

If target runtime is not setup, create a new target runtime pointing to geronimo installation directory. For more information, look at the geronimo documentation that explains setting up eclipse plugin for geronimo and setting up runtime environment. This setup is required to resolve class dependencies during compilation.

Click on the Next button.

Unable to render embedded object: File (3.jpg) not found.

4. Select the checkboxes as given in the figure below and click on the Next button.

Unable to render embedded object: File (4.jpg) not found.

5. Select the checkboxes as given in the below figure and click on the Next button.

Unable to render embedded object: File (5.jpg) not found.

6. Provide the following values in the textboxes and click on the Finish button.

Unable to render embedded object: File (6.jpg) not found.

7. Right click on the CurrencyEJB project and navigate to New => Class option. Provide the following values in the New Java Class wizard and click on Finish button.

Unable to render embedded object: File (7.jpg) not found.

8. Copy the following contents into Currency.java. The Currency.java uses various JPA annotations to mark the class as an entity and designates database table to persist the attribute values.

Currency.java
package sample.jpa.currency;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="CurrencyRateTable")
public class Currency implements Serializable {
	
 @Id
  public String currencyName;
  public String countryName;
  public double rateAgstUSD;

  public Currency(){
  }

  public String getCurrencyName() {
   return currencyName;
  }

  public void setCurrencyName(String currencyName) {
   this.currencyName = currencyName;
  }

  public String getCountryName() {
   return countryName;
  }

  public void setCountryName(String countryName) {
   this.countryName = countryName;
  }

  public double getRateAgstUSD() {
   return rateAgstUSD;
  }

  public void setRateAgstUSD(double rateAgstUSD) {
   this.rateAgstUSD = rateAgstUSD;
  }
}

9. Similarly, create the interface CurrencyInterface.java and copy the following contents. This is the
stateless session bean interface.

CurrencyInterface.java
package sample.jpa.currency;
import java.util.List;
public interface CurrencyInterface {
 public Currency createCurrency(String currencyName, 
                                String countryName, 
                                double rateAgstUSD);
	
 public List listCurrencies();
	
 public Currency updateCurrency(String currencyName, 
                                double rateAgstUSD);
	
 public void deleteCurrency(String currencyName);
	
 public Currency retrieveCurrency(String currencyName);
	
}

10. Similarly, create CurrencyBean.java and copy the following contents. This class is the bean class that implements CurrencyInterface.java interface.

CurrencyBean.java
package sample.jpa.currency;

import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@Local
public class CurrencyBean implements CurrencyInterface{

 @PersistenceContext(unitName="CurrencyRateUnit")
 private EntityManager em;
	
 public Currency createCurrency(String currencyName, 
                                String countryName, 
			        double rateAgstUSD){
		
  Currency crt1 = em.find(Currency.class, currencyName);

  if(crt1 != null) 
   throw new IllegalArgumentException
   ("Currency already exists: Currency Name ("+currencyName+")");

  Currency crt = new Currency();
		
  crt.setCountryName(countryName);
  crt.setCurrencyName(currencyName);
  crt.setRateAgstUSD(rateAgstUSD);
		

  System.out.println("Persisting Currency Rate entity: 
   (Currency = "+currencyName+")");

  em.persist(crt);

  System.out.println("Persisted successfully Currency entity: 
   (Currency = "+currencyName+")");
  return crt;
	
 }
	
 public List listCurrencies(){
  if(em == null) System.out.println("em is null!!!");

  Query q = em.createQuery("SELECT crt FROM Currency crt");

  List currList = q.getResultList();

  return currList;
 }
	
 public Currency updateCurrency(String currencyName, 
                                double rateAgstUSD){
  if(em == null) System.out.println("em is null!!!");
		
  Currency crt1 = em.find(Currency.class, currencyName);

  if(crt1 == null) throw new IllegalArgumentException
   ("Currency not found: Currency Name ("+currencyName+")");

  crt1.setRateAgstUSD(rateAgstUSD);

  return crt1;
		 
 }
	
 public void deleteCurrency(String currencyName){
  if(em == null) System.out.println("em is null!!!");

  Currency crt1 = em.find(Currency.class, currencyName);

  if(crt1 == null) throw new 
   IllegalArgumentException
   ("Currency not found: Currency Name ("+currencyName+")");

  em.remove(crt1);
 }

 public Currency retrieveCurrency(String currencyName){
  Currency crt1 = em.find(Currency.class, currencyName);
  return crt1;
 }
	
}

11. As outlined above, right click on the META-INF directory of CurrencyEJB project and create persistence.xml. Copy the following contents into persistence.xml.

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

 <persistence-unit name="CurrencyRateUnit">
 <description>JPA JSF Sample</description>
 <provider>
  org.apache.openjpa.persistence.PersistenceProviderImpl
 </provider>
 <class>
  sample.jpa.currency.Currency
 </class>

 <properties>
  <property name="openjpa.ConnectionURL" 
   value="jdbc:derby:CurrencyDB" />
  <property name="openjpa.ConnectionDriverName" 
   value="org.apache.derby.jdbc.EmbeddedDriver" />
  <property name="ConnectionUserName" value="app" />
  <property name="openjpa.jdbc.SynchronizeMappings" value="false" />
 </properties>
 </persistence-unit>
</persistence>

12. Since we are going to use EJB annotations, the META-INF/ejb-jar.xml will not have any declarations. The contents of the META-INF/openejb-jar.xml file should be as below. Otherwise, modify it accordingly.

openejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2" 
 xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2" 
 xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" 
 xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
  <sys:environment>
    <sys:moduleId>
      <sys:groupId>Currency</sys:groupId>
      <sys:artifactId>JPA-EJB</sys:artifactId>
      <sys:version>1.0</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
  </sys:environment>
  <enterprise-beans/>
  </openejb-jar>

13. Finally the project CurrencyEJB should like as below.

Unable to render embedded object: File (8.jpg) not found.

Creating web application with JSF configuration

1. Right click on the Project Explorer and select New => Project. This will open New Project wizard. Select Dynamic Web Project under option Web. Click on the Next button.

Unable to render embedded object: File (9.jpg) not found.

2. Provide the values as given in the figure below on the New Dynamic Web Project wizard. Please note that Add project to an EAR checkbox is check to add this web project to CurrencyEAR created earlier.

Unable to render embedded object: File (10.jpg) not found.

3. In the next screen, select the Version values as given in the below figure and click on the Next button. Please note that the Java Server Faces check box is checked and the version values should be 1.2.

Unable to render embedded object: File (11.jpg) not found.

4. Check on the Generate Deployment Descriptor checkbox and click on the Next button. On the next screen, configure the deployment plan as follows. After this click on the Next button.

Unable to render embedded object: File (12.jpg) not found.

5. On the JSF Capabilities window, select the second radio button and click on the new button.

Unable to render embedded object: File (13.jpg) not found.

6. The next wizard Create JSF Implementation Library suggests to create JSF Implementation library. Give the library name as JSFCustomLibrary and add the following jars. click on the Finish button once done. See the figure below.

  • <geronimo_home>\repository\commons-beanutils\commons-beanutils\1.6.1\commons-beanutils-1.6.1.jar
  • <geronimo_home>\repository\commons-collections\commons-collections\3.1\commons-collections-3.1.jar
  • <geronimo_home>\repository\commons-digester\commons-digester\1.8\commons-digester-1.8.jar
  • <geronimo_home>\repository\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar
  • <geronimo_home>\repository\org\apache\myfaces\core\myfaces-api\1.2.2\myfaces-api-1.2.2.jar
  • <geronimo_home>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.jar

Unable to render embedded object: File (14.jpg) not found.

7. On the JSF Capabilities window change the value of URL Mapping Patterns to *.jsf and click on the Finish button.

Unable to render embedded object: File (15.jpg) not found.

Right click on the WEB-INF folder and create a tld folder and copy the following files into it.

  • <geronimo_home>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.jar\META-INF\
    myfaces-html.tld
  • <geronimo_home>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.jar\META-INF\
    myfaces_core.tld

8. The above steps create a new web project with JSF capabilities. Now we can create the required JSPs, JSF managed beans and configure the navigation rules in the WEB_INF/faces-config.xml. Right click on the __CurrencyWEB => WebContent_and create the following JSPs as given figure below. The contents of the JSPs are also provided below the figure.

Unable to render embedded object: File (16.jpg) not found.

index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    <jsp:forward page="/listCurrencies.jsf" />
  </body>
</html>
listCurrencies.jsp
<%@ page language="java" %>

<%@ taglib uri="/WEB-INF/tld/myfaces-html.tld" prefix="h" %>
<%@ taglib uri="/WEB-INF/tld/myfaces_core.tld" prefix="f" %>  

<%
String path = request.getContextPath();
String basePath = request.getScheme()+
 "://"+request.getServerName()+
 ":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
 <base href="<%=basePath%>">
  <title>List of currencies</title>
</head>
  
<body>
 <f:view>
  <h:form id="currencyList">
  <h:dataTable id="currencies" 
   value="#{CurrencyListJSFBean.currencies}" 
   var="currency" 
   border="1">   
   <h:column>
    <f:facet name="header">
     <h:outputText  value="Currency Name"/>
    </f:facet>
    <h:outputText value="#{currency.currencyName}" />
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText  value="Country Name"/>
    </f:facet>
    <h:outputText value="#{currency.countryName}" />
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText  value="Rate Against USD"/>
    </f:facet>
    <h:outputText value="#{currency.rateAgstUSD}" />
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText  value="Edit"/>
    </f:facet>
    <h:commandLink id="Edit" 
     action="editCurrency" 
     actionListener="#{CurrencyJSFBean.selectCurrency}">
      <h:outputText value="Edit" />
      <f:param id="editCurrency" 
       name="currencyName" 
       value="#{currency.currencyName}" />
    </h:commandLink>
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText  value="Delete"/>
    </f:facet>
    <h:commandLink id="Delete" 
     action="deleteCurrency" 
     actionListener="#{CurrencyListJSFBean.deleteCurrency}">
     <h:outputText value="Delete" />
     <f:param id="deleteCurrency" 
      name="currencyName" 
      value="#{currency.currencyName}" />
    </h:commandLink>
   </h:column>
  </h:dataTable> 
  <h:commandLink id="Add" 
   action="insertCurrency" 
   actionListener="#{CurrencyJSFBean.initCurrency}">
   <h:outputText value="Add a Currency" />
   </h:commandLink>
  </h:form>
  </f:view>
</body>
</html>
insertCurrencyjsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/tld/myfaces-html.tld" prefix="h" %>
<%@ taglib uri="/WEB-INF/tld/myfaces_core.tld" prefix="f" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
 "://"+request.getServerName()+
 ":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
 <base href="<%=basePath%>">
 <title>Add Currency</title>
</head>
  
<body>
 <f:view>
  <h:form>
   <h:inputHidden id="id" value="#{CurrencyJSFBean.currencyName}"/>
    <h:panelGrid columns="2" border="1">
     <h:outputText value="Currency Name :" />
     <h:inputText id="currencyName" 
      value="#{CurrencyJSFBean.currencyName}">
     </h:inputText>
     <h:outputText value="Country Name :" />
     <h:inputText id="countryName" 
      value="#{CurrencyJSFBean.countryName}">
     </h:inputText>
     <h:outputText value="Rate Against USD :" />
     <h:inputText id="rateAgstUSD" 
      value="#{CurrencyJSFBean.rateAgstUSD}">
     </h:inputText>
    </h:panelGrid>
		
  <h:messages id="errors" 
   style="color:red;font-weight:bold" 
   layout="table" />
   <h:commandButton value="Save" 
    action="listCurrencies" 
    actionListener="#{CurrencyJSFBean.insertCurrency}" />

  </h:form>
 </f:view>
</body>
</html>
editCurrency.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/tld/myfaces-html.tld" prefix="h" %>
<%@ taglib uri="/WEB-INF/tld/myfaces_core.tld" prefix="f" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
 "://"+request.getServerName()+
 ":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
 <base href="<%=basePath%>">
 <title>Edit Currency</title>
</head>
  
<body>
 <f:view>
  <h:form>
   <h:inputHidden id="id" value="#{CurrencyJSFBean.currencyName}"/>
   <h:panelGrid columns="2" border="1">
    <h:outputText value="Currency Name :" />
    <h:outputText value="#{CurrencyJSFBean.currencyName}" />
    <h:outputText value="Country Name :" />
    <h:outputText value="#{CurrencyJSFBean.countryName}" />
    <h:outputText value="Rate Against USD :" />
    <h:inputText id="rateAgstUSD" 
     value="#{CurrencyJSFBean.rateAgstUSD}">
    </h:inputText>
   </h:panelGrid>
		
   <h:messages id="errors" 
    style="color:red;font-weight:bold" 
    layout="table" />
   
   <h:commandButton value="Save" 
    action="listCurrencies" 
    actionListener="#{CurrencyJSFBean.saveCurrency}" />

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

9. Create two JSF managed beans CurrencyJSFBean.java and CurrencyListJSFBean.java as follows. Right click on the CurrencyWEB and select CurrencyWEB => New => Class.

Unable to render embedded object: File (17.jpg) not found.

In the New Java Class wizard, provide the values as given in the figure and click on the Finish button.

Unable to render embedded object: File (18.jpg) not found.

Copy the below contents into CurrencyListJSFBean.java

CurrencyListJSFBean.java
package sample.jsf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.LinkRef;

import sample.jpa.currency.Currency;
import sample.jpa.currency.CurrencyInterface;



public class CurrencyListJSFBean {
 private CurrencyInterface ci;
 private Collection currencies;

 public CurrencyListJSFBean() throws Exception{
  Context ctx  = new InitialContext();
  System.out.println("Instantiating bean...");
  Object obj = ctx.lookup("java:comp/env/ejb/CurrencyInterface");
  System.out.println((obj instanceof LinkRef));
  ci = (CurrencyInterface)obj;
  System.out.println("Got the  bean...");
  init();
 }

 public Collection getCurrencies() {
  return currencies;
 }

 public void setCurrencies(Collection currencies) {
  this.currencies = currencies;
 }
	
 public void loadCurrencies()throws Exception{
  init();
 }
	
 private void init()throws Exception{
  List list  = ci.listCurrencies();
  Iterator it  = list.iterator();
  this.currencies = new ArrayList();
  while (it.hasNext()){
   Currency currency = (Currency)it.next();
   CurrencyJSFBean cjb = new CurrencyJSFBean();
   cjb.setCountryName(currency.getCountryName());
   cjb.setCurrencyName(currency.getCurrencyName());
   cjb.setRateAgstUSD(currency.getRateAgstUSD());

   System.out.println(currency.getCountryName()+ 
    " : " +currency.getCurrencyName()+ " : "+ 
    currency.getRateAgstUSD());;
			
   currencies.add(cjb);
  }
 }
	
 public void deleteCurrency (ActionEvent event){
  UIParameter component = (UIParameter) event.getComponent().findComponent("deleteCurrency");
  String curencyNameDel = (String) component.getValue();
  System.out.println("Currency Name to be deleted = "+curencyNameDel);
  ci.deleteCurrency(curencyNameDel);
		
  Iterator it = this.getCurrencies().iterator();
  while (it.hasNext()){
   CurrencyJSFBean cjb = (CurrencyJSFBean)it.next();
   if(cjb.getCurrencyName().equals(curencyNameDel)){
    it.remove();
   }
  }
 }
}

Similarly, create CurrencyJSFBean.java in the same package and copy the following contents into it.

CurrencyJSFBean.java
package sample.jsf;

import java.util.Iterator;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.LinkRef;

import sample.jpa.currency.Currency;
import sample.jpa.currency.CurrencyInterface;


public class CurrencyJSFBean {

 private CurrencyInterface ci;

 private String currencyName;
 private String countryName;
 private double rateAgstUSD;

 public CurrencyJSFBean()throws Exception{
  Context ctx  = new InitialContext();
  System.out.println("Instantiating bean...");
  Object obj = ctx.lookup("java:comp/env/ejb/CurrencyInterface");
  System.out.println((obj instanceof LinkRef));
  ci = (CurrencyInterface)obj;
  System.out.println("Got the  bean...");
 }


 public String getCurrencyName() {
  return currencyName;
 }

 public void setCurrencyName(String currencyName) {
  System.out.println("Set currencyName="+currencyName);
  this.currencyName = currencyName;
 }

 public String getCountryName() {
 return countryName;
 }

 public void setCountryName(String countryName) {
  System.out.println("Set countryName="+countryName);
  this.countryName = countryName;
 }

 public double getRateAgstUSD() {
  return rateAgstUSD;
 }

 public void setRateAgstUSD(double rateAgstUSD) {
  System.out.println("Set rateAgstUSD="+rateAgstUSD);
  this.rateAgstUSD = rateAgstUSD;
 }

 public void selectCurrency(ActionEvent event){

  UIParameter component = (UIParameter) 
              event.getComponent().findComponent("editCurrency");
  String curencyName = (String) component.getValue();

  Currency c = ci.retrieveCurrency(curencyName);

  this.setCountryName(c.getCountryName());
  this.setCurrencyName(c.getCurrencyName());
  this.setRateAgstUSD(c.getRateAgstUSD());
 }

 public void saveCurrency(ActionEvent event){
  ci.updateCurrency(this.getCurrencyName(), this.getRateAgstUSD());

 }

 public void initCurrency(ActionEvent event){
  this.setCountryName("");
  this.setCurrencyName("");
  this.setRateAgstUSD(0.0);
 }

 public void insertCurrency(ActionEvent event){
  ci.createCurrency(this.currencyName, countryName, rateAgstUSD);
 }

}

10. Open the WEB-INF/faces-config.xml and copy the below contents to it.

The faces-config.xml can also be configured using JSF wizards. In this case, we have copied the already prepared JSF configuration in to the faces-config.xml file.

faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>

<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_1_2.xsd"
 version="1.2">

 <navigation-rule>
  <description>List Currencies</description>
  <from-view-id>/listCurrencies.jsp</from-view-id>
  <navigation-case>
   <from-outcome>editCurrency</from-outcome>
   <to-view-id>/editCurrency.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>

 <navigation-rule>
  <description>Edit Currency</description>
  <from-view-id>/editCurrency.jsp</from-view-id>
  <navigation-case>
   <from-outcome>listCurrencies</from-outcome>
   <to-view-id>/listCurrencies.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>

 <navigation-rule>
  <description>List Currencies</description>
  <from-view-id>/listCurrencies.jsp</from-view-id>
  <navigation-case>
   <from-outcome>insertCurrency</from-outcome>
   <to-view-id>/insertCurrency.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>

 <navigation-rule>
  <description>Insert Currencies</description>
  <from-view-id>/insertCurrency.jsp</from-view-id>
  <navigation-case>
   <from-outcome>listCurrencies</from-outcome>
   <to-view-id>/listCurrencies.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>

 <navigation-rule>
  <description>List Currencies</description>
  <from-view-id>/listCurrencies.jsp</from-view-id>
  <navigation-case>
   <from-outcome>deleteCurrency</from-outcome>
  <to-view-id>/listCurrencies.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>


 <managed-bean>
  <description>
   Currency List Bean
  </description>
  <managed-bean-name>
   CurrencyListJSFBean
  </managed-bean-name>
  <managed-bean-class>
   sample.jsf.CurrencyListJSFBean
  </managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope> 
 </managed-bean>

 <managed-bean>
  <description>
   Currency Bean
  </description>
  <managed-bean-name>CurrencyJSFBean</managed-bean-name>
  <managed-bean-class>
   sample.jsf.CurrencyJSFBean
  </managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope> 
 </managed-bean>

</faces-config>

11. The web.xml and the geronimo-web.xml files are as follows. In the web.xml the ejb reference is
mapped to the CurrencyBean.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 id="WebApp_ID" version="2.5">

 <display-name>CurrencyWEB</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>
   javax.faces.webapp.FacesServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <ejb-local-ref>
  <ejb-ref-name>ejb/CurrencyInterface</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <local>
   sample.jpa.currency.CurrencyInterface
  </local>
  <ejb-link>CurrencyBean</ejb-link>
 </ejb-local-ref>

 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>
</web-app>
geronimo-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" 
 xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2" 
 xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" 
 xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
 
 <sys:environment>
  <sys:moduleId>
   <sys:groupId>Currency</sys:groupId>
   <sys:artifactId>JPA-WEB</sys:artifactId>
   <sys:version>1.0</sys:version>
   <sys:type>car</sys:type>
  </sys:moduleId>
 </sys:environment>

 <context-root>/CurrencyWEB</context-root>
</web-app>

11. Right click on the CurrencyWEB project and click on Properties to open Properties for CurrencyWEB wizard. Click on the Java Build Path and Projects tab. Click on the Add button and add CurrencyEJB project. Finally, click on the OK button on Properties for CurrencyWEB wizard. This is required because, CurrencyWEB projects looks up CurrencyInterface ejb in the CurrencyEJB project. To resolve the dependency during compilation, the EJB project has to be added to the build path of the WEB project.

12. Finally export the CurrencyEAR file.

Deploying the (ear) application

1. Deploy the EAR file as follows

C:\Geronimo-2.1\bin>deploy.bat --user system --password manager deploy c:\temp\CurrencyEAR.ear
Using GERONIMO_BASE:   C:\Geronimo-2.1
Using GERONIMO_HOME:   C:\Geronimo-2.1
Using GERONIMO_TMPDIR: var\temp
Using JRE_HOME:        C:\SDK-May-31-2007\jre
    Deployed default/CurrencyEAR/1.0/car
      `-> CurrencyWEB.war @ /CurrencyWEB
      `-> CurrencyEJB.jar 

Running the application

1. Open a browser window and hit the URL as http://localhost:8080/CurrencyWEB/

Unable to render embedded object: File (19.jpg) not found.

This page displays the list of currencies currently in the database. For each currency, the page displays Currency Name, Country Name and Rate Against USD values. It also provides link to edit and delete a particular currency.

2. Click on the Add a Currency link at the bottom of the table. It will open up the following screen. Enter the values and click on the Save button. This will take back to the list of currencies with the new entry in the list.

Unable to render embedded object: File (20.jpg) not found.

Similarly, the Edit and Delete operations can also be performed.

Powered by Atlassian Confluence (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request

Unsubscribe or edit your notifications preferences