Showing posts with label jsf 2.0. Show all posts
Showing posts with label jsf 2.0. Show all posts

5/16/2011

5. Add persistence: Example App using Seam 3, RichFaces 4 on eclipse

Building example application seam 3 + richfaces 4 series
5. Add persisistence



1. Add persistence.xml in WebContent/WEB-INF/classes/META-INF

1.1 create classes/META-INF directory on WebContent/WEB-INF directory
1.2 create persistence.xml in WebContent/WEB-INF/classes/META-INF
(you need to configure this file to fit with your environment)



<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/testDatasource</jta-data-source>
<properties>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.username" value="test"/>
<property name="hibernate.connection.password" value="test"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect" />
</properties>
</persistence-unit>

</persistence>




2. Add test-ds.xml (datasource) in JBOSS deploy directory
(you need to configure this file to fit with your environment)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
"http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
<local-tx-datasource>
<jndi-name>testDatasource</jndi-name>
<use-java-context>true</use-java-context>
<connection-url>jdbc:oracle:thin:@hostname:1521:SID</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>test</user-name>
<password>test</password>
<max-pool-size>100</max-pool-size>
</local-tx-datasource>
</datasources>



3. If oracle, make sure ojdbc15-xxx.jar or relevant file is placed in $JBOSS_HOME/server/default/lib (if using default)
For other database place driver jar file in there.

4. create data model (this is just example. you will need to setup your own database and table)

I placed ZoneCodes.java in seam3-test/src/com/test directory



package com.test;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "zone_codes")
public class ZoneCodes {

@Id
@Column(name = "zone_code")
private String code;

@Column(name = "zone_description")
private String description;

public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}



5. create controller

I placed ZoneController.java in seam3-test/src/com/test directory


package com.test;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateful;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.seam.faces.context.RenderScoped;
import org.jboss.seam.international.status.Messages;

/**
* This class performs search and outject result
*/
@Named
@Stateful
@RenderScoped
public class ZoneController {
@PersistenceContext
private EntityManager em;

@Inject
Messages messages;

private List zoneCodes = new ArrayList();

/**
* Perform search
* This retrieves all ZoneCodes from database
*/
public void search() {
Query q = em.createQuery("from ZoneCodes");
zoneCodes = q.getResultList();
messages.info("Search completed. {0} records found", zoneCodes.size());
}

/**
* Reset results
*/
public void reset() {
zoneCodes = new ArrayList();
}

// out-jection
@Produces
@Named
public List getZoneCodes() {
return zoneCodes;
}
}


The difference between Seam 2 and Seam 3 is quite significant.


Most of major concept in Seam 2 are absorbed in CDI (jboss 6 comes with it), and Seam 3 is providing extras on top of it.


So, when I made seam 3 working on JBoss 6, most of major seam 2 concepts absorbed into CDI. That ands up some people complaining that this article is not seam 3 exemple. Instead it is just CDI example.


I agree. So, I had to provide Seam 3 is still working on my example. Hence, I added couple of 'seam 3' specific features in it.



6. Add index_db.xhtml in WebContent directory



<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<f:view>
<h:head>
</h:head>
<h:body styleClass="page">
<h1>Persistence test</h1>

<h:form id="zoneSelectionForm">
<div>
<h:form id="searchForm" prependId="false">
<a4j:commandButton id="search" value="Search" action="#{zoneController.search}" render=":searchResults"/>
#{' '}
<a4j:commandButton id="reset" value="Clear" action="#{zoneController.reset}" render=":searchResults"/>
</h:form>
</div>

<h:panelGroup id="searchResults">
<h:messages />
<h:dataTable id="zones" value="#{zoneCodes}" var="zone" rendered="#{not empty zoneCodes}">
<h:column>
<f:facet id="nameFct" name="header">Code</f:facet>
#{zone.code}
</h:column>
<h:column>
<f:facet id="addrFct" name="header">Description</f:facet>
#{zone.description}
</h:column>
</h:dataTable>
</h:panelGroup>
</h:form>

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

</html>


7. deploy it onto Jboss 6

8. http://localhost:8080/seam3-test/index_db.xhtml

Sample output is like below


4. add primfaces: Example App using Seam 3, RichFaces 4 on eclipse

Building example application seam 3 + richfaces 4 series
4. Add primefaces

1. add jar file
This is quite simple.

You can just add primefaces-3.0.M1.jar (or latest) in WebContent/WEB-INF/lib directory
, and you are ready to use it.

2. add index_primefaces.xml in WebContent directory

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:p="http://primefaces.prime.com.tr/ui">
<f:view>
<h:head>
</h:head>
<h:body styleClass="page">
<h1>Primefaces test</h1>

<div>
<p:tooltip global="true"/>

<h:panelGrid columns="1" cellpadding="5">
<h:form>
<p:commandButton value="Submit" title="Click to save" type="button" />

<h:outputLink value="#" title="Go to">
<h:outputText value="Link" />
</h:outputLink>

<p:inputText title="This is a required field" />
</h:form>
</h:panelGrid>
</div>
</h:body>
</f:view>
</html>



3. deploy it onto Jboss 6

4. http://localhost:8080/seam3-test/index_primefaces.xhtml

3. add taglib: Example App using Seam 3, RichFaces 4 on eclipse

Building example application seam 3 + richfaces 4 series
3. Add custom taglib


If you are already using custome taglib on jsf1.2 with facelet, there is nothing changed. You can reuse existing taglib as it is.

1. To setup seam 3 + richfaces 4 check

Building example application seam 3 + richfaces 4 series
3. Add custom taglib


2. add following configuration on web.xml

<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/taglib_test/taglib_test.taglib.xml</param-value>
</context-param>


3. create custom tag

3.1 create taglib_test directory on WebContent
3.2 create taglib_test.taglib.xml file in WebContent/taglib_test directory

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://test.com/tl_test</namespace>
<tag>
<tag-name>test_panel</tag-name>
<source>taglib_test.xhtml</source>
</tag>
</facelet-taglib>

3.3 create taglib_test.xhtml file in WebContent/taglib_test directory

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:rich="http://richfaces.org/rich">
<rich:panel header="Tag lib testing">
This is testing for custom taglib. This is remained same as before in JSF 1.2.
</rich:panel>
</ui:composition>


4. create index_with_taglib.xhtml in WebContent directory

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:rich="http://richfaces.org/rich"
xmlns:tt="http://test.com/tl_test"
template="template.xhtml">

<ui:define name="content">
<h1>Seam 3 taglib test</h1>

<tt:test_panel />

</ui:define>
</ui:composition>


5. deploy it onto Jboss 6

6. http://localhost:8080/seam3-test/index_with_taglib.xhtml

2. Apply template: Example App using Seam 3, RichFaces 4 on eclipse

Building example application seam 3 + richfaces 4 series
2. Apply template


1. To setup seam 3 + richfaces 4 check

Building example application seam 3 + richfaces 4


2. add template.xhtml on WebContent directory

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:head>
</h:head>
<h:body>
<ui:insert name="content"/>
</h:body>
</f:view>
</html>

3. add index_with_template.xhtml on WebContent directory

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
template="template.xhtml">

<ui:define name="content">
<h1>Seam 3 template test</h1>

<rich:panel header="Richfaces running on JBoss AS 6">
<ul>
<li>RichFaces is a component library for JSF and an advanced framework</li>
<li>for easily integrating AJAX capabilities into business applications.</li>
</ul>
</rich:panel>
</ui:define>
</ui:composition>


4. deploy it onto Jboss 6

5. http://localhost:8080/seam3-test/index_with_template.xhtml

1. Setup: Example App Seam 3, RichFaces 4 on eclipse without maven

Building example application seam 3 + richfaces 4 series
1. setup seam 3 + richfaces 4


1. Start eclipse
2. New -> Dynamic Web Project




3. copy the following files into WebContent/WEB-INF/lib

3.1 seam 3 dependent files

commons-beanutils-1.8.0.jar
commons-digester-2.1.jar
commons-logging-1.1.1.jar
joda-time-1.6.jar
picketlink-idm-api-1.5.0.Alpha02.jar
picketlink-idm-common-1.5.0.Alpha02.jar
picketlink-idm-core-1.5.0.Alpha02.jar
picketlink-idm-spi-1.5.0.Alpha02.jar
seam-catch-3.0.0.Final.jar
seam-config-xml-3.0.0.Final.jar
seam-faces-3.0.0.Final.jar
seam-international-3.0.0.Final.jar
seam-persistence-3.0.0.Final.jar
seam-security-3.0.0.Final.jar
seam-servlet-3.0.0.Final.jar
seam-solder-3.0.0.Final.jar


3.2 richfaces related files

cssparser-0.9.5.jar
guava-r09.jar
richfaces-components-api-4.0.0.Final.jar
richfaces-components-ui-4.0.0.Final.jar
richfaces-core-api-4.0.0.Final.jar
richfaces-core-impl-4.0.0.Final.jar
sac-1.3.jar



4. create web.xml on WebContent/WEB-INF directory


<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_0.xsd"
version="3.0">

<display-name>Seam test</display-name>

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

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>


</web-app>


5. Add index.xhtml on WebContent directory


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:rich="http://richfaces.org/rich">
<f:view>
<h:head>
</h:head>
<h:body styleClass="page">
<h1>Seam 3 test</h1>

<rich:panel header="Richfaces running on JBoss AS 6">
<ul>
<li>RichFaces is a component library for JSF and an advanced framework</li>
<li>for easily integrating AJAX capabilities into business applications.</li>
</ul>
</rich:panel>
</h:body>
</f:view>
</html>

6. deploy it onto Jboss 6

7. http://localhost:8080/seam3-test/index.xhtml