...
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.
Code Block |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | Currency.java |
---|
borderStyle | solid |
---|
| 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.
Code Block |
---|
| JAVA | borderStyle | solid |
---|
title | CurrencyInterface.java |
---|
borderStyle | solid |
---|
| 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.
Code Block |
---|
| JAVA | borderStyle | solid |
---|
title | CurrencyBean.java |
---|
borderStyle | solid |
---|
| 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
.
Code Block |
---|
borderStyle | solid |
---|
title | persistence.xml |
---|
borderStyle | solid |
---|
|
<?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.
Code Block |
---|
borderStyle | solid |
---|
title | openejb-jar.xml |
---|
borderStyle | solid |
---|
|
<?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>
|
...
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 in the below screen shot. The contents of the JSPs are also provided below.
Code Block |
---|
borderStyle | solid |
---|
title | index.jsp |
---|
borderStyle | solid |
---|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<jsp:forward page="/listCurrencies.jsf" />
</body>
</html>
|
Code Block |
---|
borderStyle | solid |
---|
title | listCurrencies.jsp |
---|
borderStyle | solid |
---|
|
<%@ 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>
|
Code Block |
---|
title | insertCurrencyjsp |
---|
borderStyle | solid | title | 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>
|
Code Block |
borderStyle |
---|
solid | title | editCurrency.jsp |
---|
borderStyle | solid |
---|
|
<%@ 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>
|
...
Copy the below contents into CurrencyListJSFBean.java
Code Block |
---|
| JAVA | borderStyle | solid |
---|
title | CurrencyListJSFBean.java |
---|
borderStyle | solid |
---|
| 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.
Code Block |
---|
| JAVA | borderStyle | solid |
---|
title | CurrencyJSFBean.java |
---|
borderStyle | solid |
---|
| 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);
}
}
|
...
Note |
---|
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. |
Code Block |
---|
borderStyle | solid |
---|
title | faces-config.xml |
---|
borderStyle | solid |
---|
|
<?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 ejb/CurrencyInterface
is mapped to the CurrencyBean
.
Code Block |
---|
borderStyle | solid |
---|
title | web.xml |
---|
borderStyle | solid |
---|
|
<?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>
|
Code Block |
---|
borderStyle | solid |
---|
title | geronimo-web.xml |
---|
borderStyle | solid |
---|
|
<?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>
|
...