Spring + Hibernate + JSF + Primeface Intergration Project

Used Technologies:

  1. Jdk 1.7.0
  2. Spring 3.1.1
  3. Hibernate 4.1.0
  4. JSF 2.1.6
  5. Primefaces 3.1.1
  6. PostgreSQL 1.16.1
  7. Tomcat 7.0
  8. Spring Tool Suite – STS 3.2.0

  I. Create maven project with archetype: maven-archetype-webapp

maven

And, folder structure:

folder structure

II. Add libraries: pom.xml

<project xmlns=“http://maven.apache.org/POM/4.0.0″ xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”</pre>
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>com.ant</groupId>

<artifactId>pbacklog</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>pbacklog Maven Webapp</name>

<url>http://maven.apache.org</url&gt;

<properties>

<spring.version>3.1.1.RELEASE</spring.version>

</properties>

<repositories>

<repository>

<id>prime-repo</id>

<name>PrimeFaces Maven Repository</name>

<url>http://repository.primefaces.org</url&gt;

<layout>default</layout>

</repository>

</repositories>

<dependencies>

<!– Spring 3 dependencies –>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

<!– JSF dependencies –>

<dependency>

<groupId>com.sun.faces</groupId>

<artifactId>jsf-api</artifactId>

<version>2.1.6</version>

</dependency>

<dependency>

<groupId>com.sun.faces</groupId>

<artifactId>jsf-impl</artifactId>

<version>2.1.6</version>

</dependency>

<!– Hibernate dependencies –>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.1.0.Final</version>

</dependency>

<dependency>

<groupId>javassist</groupId>

<artifactId>javassist</artifactId>

<version>3.12.1.GA</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<!– Primefaces dependency –>

<dependency>

<groupId>org.primefaces</groupId>

<artifactId>primefaces</artifactId>

<version>3.1.1</version>

</dependency>

<!– PostgreSQL dependency –>

<dependency>

<groupId>postgresql</groupId>

<artifactId>postgresql</artifactId>

<version>9.1-901.jdbc4</version>

</dependency>

<!– Common JDBC dependency –>

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.4</version>

</dependency>

<!– JUnit dependency –>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<finalName>pbacklog</finalName>

</build>

</project>
<pre>

III. Create POJO class


package com.ant.pbacklog.model;

import java.io.Serializable;

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

@Entity
@Table(name = “UserStory”)
public class UserStory implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name=”id”)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long userStoryId;

private String name;

@Column(nullable = false, length = 1000)
private String description;

@Column(nullable = false)
private int value;

private String status;
public Long getUserStoryId() {
return userStoryId;
}

public void setUserStoryId(Long userStoryId) {
this.userStoryId = userStoryId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}

IV. Create mananged bean class

package com.ant.pbacklog.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ant.pbacklog.model.UserStory;
import com.ant.pbacklog.service.UserStoryService;
@Component(“pblBean”)
@Scope(“session”)
public class ProducBacklogBean implements Serializable{

private static final long serialVersionUID = 1L;
private Long userStoryId;
private String name;
private String description;
private int value;
private String status;

private List<UserStory> usList;

public List<UserStory> getUsList() {
usList=new ArrayList<UserStory>();
usList=userStoryService.getAllUserStories();
return usList;
}
public void setUsList(List<UserStory> usList) {
this.usList = usList;
}
@Autowired
UserStoryService userStoryService;

public void addUserStory(){
UserStory us = new UserStory();
us.setName(getName());
us.setDescription(getDescription());
us.setStatus(getStatus());
us.setValue(getValue());
userStoryService.addUserStory(us);
}
public void delete(Long userStoryId){
userStoryService.deleteUserStory(userStoryId);
}
public Long getUserStoryId() {
return userStoryId;
}
public void setUserStoryId(Long userStoryId) {
this.userStoryId = userStoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

}

  V.  Create classes for Bussiness Logic Layer

package com.ant.pbacklog.service;

import java.util.List;

import com.ant.pbacklog.model.UserStory;

public interface UserStoryService {

public void addUserStory(UserStory us) ;

public void deleteUserStory(Long userStoryId);

public List<UserStory> getAllUserStories() ;
<div>

}

—-----
package com.ant.pbacklog.service;

import java.io.Serializable;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.ant.pbacklog.dao.UserStoryDao;

import com.ant.pbacklog.model.UserStory;

@Service

@Transactional

public class UserStoryServiceImpl implements UserStoryService,Serializable {

private static final long serialVersionUID = 1L;

@Autowired

UserStoryDao userStoryDao;

public void addUserStory(UserStory us) {

userStoryDao.addUserStory(us);

}

public void deleteUserStory(Long userStoryId) {

userStoryDao.deleteUserStory(userStoryId);

}

public List<UserStory> getAllUserStories() {

return userStoryDao.getUserStories();

}

}

VI. Create classes for Data Access Layer

package com.ant.pbacklog.dao;

import java.util.List;

import com.ant.pbacklog.model.UserStory;

public interface UserStoryDao {

public void addUserStory(UserStory us);

public void deleteUserStory(Long userStoryId);

public List<UserStory> getUserStories();

}

package com.ant.pbacklog.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ant.pbacklog.model.UserStory;

@Repository(“userStoryDao”)
@Transactional
public class UserStoryDaoImpl implements UserStoryDao, Serializable{

private static final long serialVersionUID = 1L;

@Autowired
private SessionFactory sessionFactory;

public void addUserStory(UserStory us) {
Session session = sessionFactory.getCurrentSession();
session.save(us);

}

public void deleteUserStory(Long userStoryId) {
UserStory userStory = (UserStory) sessionFactory.getCurrentSession().load(UserStory.class, userStoryId);
if(userStory != null){
sessionFactory.getCurrentSession().delete(userStory);
}

}

@SuppressWarnings(“unchecked”)
public List<UserStory> getUserStories() {
Session session = sessionFactory.getCurrentSession();
List<UserStory> list = null;
try{
list = session.createQuery(“from UserStory”).list();
}
catch(HibernateException e){
e.printStackTrace();
}
return list;
}

}

VII.     Congure Spring Application Context: applicationContext.xml

<?xml version=“1.0″ encoding=“UTF-8″?></pre>
<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:tx=“http://www.springframework.org/schema/tx”

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/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd”>

<!– Enable autowire –>

<context:annotation-config />

<!– Enable component scanning –>

<context:component-scan base-package=“com.ant.pbacklog” />

<!– Data Source Declaration –>

<bean id=“myDataSource” class=“org.apache.commons.dbcp.BasicDataSource” destroy-method=“close”>

<property name=“driverClassName” value=“org.postgresql.Driver”/>

<property name=“url” value=“jdbc:postgresql://localhost:5432/userstory”/>

<property name=“username” value=“postgres”/>

<property name=“password” value=“postgres”/>

</bean>

<!– Session Factory Declaration –>

<bean id=“mySessionFactory” class=“org.springframework.orm.hibernate4.LocalSessionFactoryBean”>

<property name=“dataSource” ref=“myDataSource”/>

<property name=“packagesToScan” value=“com.ant.pbacklog.model” />

<property name=“hibernateProperties”>

<props>

<prop key=“hibernate.dialect”>org.hibernate.dialect.PostgreSQLDialect</prop>

<prop key=“hibernate.show_sql”>true</prop>

<prop key=“hibernate.enable_lazy_load_no_trans”>true</prop>

<prop key=“hibernate.hbm2ddl.auto”>create</prop>

</props>

</property>

</bean>

<!– Enable the configuration of transactional behavior based on annotations –>

<tx:annotation-driven transaction-manager=“transactionManager”/>

<!– Transaction Manager is defined –>

<bean id=“transactionManager” class=“org.springframework.orm.hibernate4.HibernateTransactionManager”>

<property name=“sessionFactory” ref=“mySessionFactory”/>

</bean>

</beans>
<pre>

  VIII.    JSF Configuration: faces-config.xml

</pre>
<?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_2_0.xsd”

version=“2.0″>

<!– JSF and Spring are integrated –>

<application>

<el-resolver>

org.springframework.web.jsf.el.SpringBeanFacesELResolver

</el-resolver>

</application>

<navigation-rule>

<from-view-id>/pages/index.xhtml</from-view-id>

</navigation-rule>

</faces-config>

IX.    Web.xml

<?xml version=“1.0″ encoding=“UTF-8″?>
<web-app version=“2.5″ 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_2_5.xsd”>
<!– The definition of the Root Spring Container shared by all Servlets and Filters –></pre>
<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<!– Creates the Spring Container shared by all Servlets and Filters –>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<listener>

<listener-class>

org.springframework.web.context.request.RequestContextListener

</listener-class>

</listener>

<!– Project Stage Level –>

<context-param>

<param-name>javax.faces.PROJECT_STAGE</param-name>

<param-value>Development</param-value>

</context-param>

<!– JSF Servlet is defined to container –>

<servlet>

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<!– Mapping with servlet and url for the http requests. –>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.faces</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.xhtml</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>/pages/index.xhtml</welcome-file>

</welcome-file-list>

</web-app>

X.Index.xhtml


<!DOCTYPE html>

<html xmlns=“http://www.w3.org/1999/xhtml”

xmlns:f=“http://java.sun.com/jsf/core”

xmlns:h=“http://java.sun.com/jsf/html”

xmlns:ui=“http://java.sun.com/jsf/facelets”

xmlns:p=“http://primefaces.org/ui” xmlns:o=“http://omnifaces.org/ui”

xmlns:sec=“http://www.springframework.org/security/tags”>

<h:head>

<title>Backlog</title>

</h:head>

<h:body>

<ui:repeat value=“#{pblBean.usList}” var=“us”>

<p:panel>

<h:outputText value=“#{us.description}”/>

</p:panel>

</ui:repeat>

</h:body>

</html>

  XI.     Done. :D

maven

And, result

maven

References:

1. http://www.onlinetechvision.com/?p=566

2. http://www.mkyong.com/jsf2/jsf-2-0-spring-hibernate-integration-example/

Download:

https://www.dropbox.com/s/auundtpjcs2gtuk/pbacklog.zip

Advertisements

Posted on 07/12/2013, in Java, JSF, Spring - Hibernate and tagged , . Bookmark the permalink. Bạn nghĩ gì về bài viết này?.

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s

Advanced programming

Tự học lập trình Android, android wear - lập trình smartwatch, Windows phone, Web service, C++, C#, java, Swift, Python, kotlin...

Tôi IT - Change the world

Research + Technical -> Programming,

juliansherman.net/

Building A Business While Having A Life

VN.NVANHUONG

No Pain No Gain

Study and Share's Blog

Việt Nam tươi đẹp...

Broly

One of the best way to learn is by writing to the paper.

%d bloggers like this: