Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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 look up ejbs to that 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.

...

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

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 environment 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 as shown in the screen shot below. Select Java EE from the list and click OK button.

...

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

Note

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.

...

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

...

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

...

6. Provide the following values in the textboxes as shown in the below screen shot and click on the Finish button.

...

Code Block
JAVA
JAVA
borderStylesolid
titleCurrencyBean.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;
 }
	
}
Note

@PersistenceContext(unitName="CurrencyRateUnit") annotation is used to inject the EntityManager object by container. In this case, the EntityManager object injected is Container Managed EntityManager object. The transaction type is JTA and the persistence scope is Transaction.
Since we have not declared any transactional attributes in the CurrencyBean, the default is Container Managed Transaction with transaction attribute being REQUIRED for the bean methods. So, the EntityManager object will join the EJB transaction whenever a method is called on the bean.

11. As outlined above, right click on the META-INF directory of CurrencyEJB project and create persistence.xml. Copy the 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.

...

Code Block
XML
XML
borderStylesolid
titleopenejb-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.

Creating web application with JSF configuration

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

...

2. Provide the values as given in the figure below screen shot 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.

...

3. In the next screen, select the Version values as given in the below figure screen shot 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.

...

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 Click on the Finish button once done. See the figure screen shot 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

...

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 in the below screen shot. The contents of the JSPs are also provided below the figure.

Code Block
ActionScript
ActionScript
borderStylesolid
titleindex.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    <jsp:forward page="/listCurrencies.jsf" />
  </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. See the below screen shot.

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

...

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

...

Code Block
XML
XML
borderStylesolid
titlegeronimo-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. Start the geronimo server and open a command window. Deploy the EAR file as follows.

No Format
bgColor#000000
borderStylesolid
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 

...

Setting up the

...

database tables

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

Image Removed

console to open admin console. Click on Console Navigation => DB Manager. This will open up DB Viewer and Run SQL portlets on the right side. Enter CurrencyDB in the Create DB textbox and click on the Create button. This will create a database by name CurrencyDB.

Image Added

2. On the Run SQL portlet, enter the below SQL command in the SQL Command/s textarea to create CURRENCYRATETABLE table. Select CurrencyDB in the Use DB combo box and click on the Run SQL button. Please see the screen shot below.

Code Block
SQL
SQL
borderStylesolid

create table CURRENCYRATETABLE (CURRENCYNAME varchar(50), COUNTRYNAME varchar(100), RATEAGSTUSD decimal (15,2));

Image Added

Enter some sample rows in the table to list the currencies later while running the application.

Running the application

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

Image Added

This page displays the list of currencies currently in the database. For each currency, the 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 Edit and delete 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.

...