Hibernate 4 Multi Tenancy Demo

Hibernate 4 : Multi Tenant Demo by Mahendra C Shinde

I. Prepare database
Using MySQL 5.5, created two schemas ‘db1’ and ‘db2’
Both contains identical table schema:

CREATE TABLE `books` (
bookId int(11) NOT NULL AUTO_INCREMENT,
title varchar(50) DEFAULT NULL,
PRIMARY KEY (`bookId`)
)

Add some data in both tables [make sure both tables contains different set of rows]

II. Prepare Hibernate Demo App
1. Create New Maven project without archetype selection
2. Add following dependencies to project:

        <dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.35</version>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-core</artifactId>
  		<version>4.3.8.Final</version>
  	</dependency>

III. Prepare hibernate configuration:

	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">mahendra</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_catalog">db1</property>
        <property name="hibernate.default_schema">db1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    	<!-- multi-tenancy configuration -->    
        <property name="hibernate.multiTenancy">SCHEMA</property>
    	<property name="hibernate.multi_tenant_connection_provider">com.mahendra.MultiTenantConnectionProviderImpl</propertyx>
    	<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    	<mapping class="com.mahendra.Book"/>

And the HibernateUtil class to build SessionFactory:

package com.mahendra;

import org.hibernate.*;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

private static SessionFactory factory = build();

private static SessionFactory build() {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
return configuration.buildSessionFactory(serviceRegistry);
}

public static SessionFactory factory() {
return factory;
}
}

IV, Prepare POJO Entity :

package com.mahendra;

import javax.persistence.*;

@Entity
@Table(name = "books")
public class Book {

@Id
@GeneratedValue
private Integer bookId;

@Column(name = "title", length = 50)
private String title;

@Override
public String toString() {
return "[" + getBookId() + "] " + getTitle();
}

public Integer getBookId() {
return bookId;
}

public void setBookId(Integer bookId) {
this.bookId = bookId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

V. Prepare ConnectionProvider for Multi-Tenancy

package com.mahendra;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.jboss.logging.Logger;

public class MultiTenantConnectionProviderImpl implements MultiTenantConnectionProvider, ServiceRegistryAwareService{

private DriverManagerConnectionProviderImpl provider =new DriverManagerConnectionProviderImpl();

private Logger log = Logger.getLogger(MultiTenantConnectionProviderImpl.class.getName());

public boolean isUnwrappableAs(Class arg0) {
return provider.isUnwrappableAs(arg0);
}

	public <T> T unwrap(Class<T> arg0) {
		return provider.unwrap(arg0);
	}


public Connection getAnyConnection() throws SQLException {
return provider.getConnection();
}

public Connection getConnection(String tenantId) throws SQLException {
Connection con = getAnyConnection();
try {
con.createStatement().execute("use "+tenantId);
log.info("Using "+tenantId+" as database schema");
}catch(SQLException ex) {
throw new HibernateException("Could not alter connection for specific schema");
}
return con;
}

public void releaseAnyConnection(Connection con) throws SQLException {
provider.closeConnection(con);

}

public void releaseConnection(String tenantId, Connection con)
throws SQLException {
try {
con.createStatement().execute("USE mysql");
System.out.println("Now, released "+tenantId);
}catch(SQLException ex) {
throw new HibernateException("Unable to reset");
}
provider.closeConnection(con);

}

public boolean supportsAggressiveRelease() {
return false;
}

public void injectServices(ServiceRegistryImplementor registry) {
Map settings = registry.getService(ConfigurationService.class).getSettings();
provider.configure(settings);
provider.injectServices(registry);

}
}

VI. The Main method to test

package com.mahendra;

import java.util.List;
import org.hibernate.*;

public class AppMain {

public static void main(String[] args) {
System.out.println("Books from db1");
loadBooksFrom("db1");
System.out.println("Books from db2");
loadBooksFrom("db2");
}

static void loadBooksFrom(String tenant) {
SessionFactory factory = HibernateUtil.factory();
Session session = factory.withOptions().tenantIdentifier(tenant).openSession();
List<Book> books = session.createQuery("from Book b").list();
for(Book b : books) {
System.out.println(b);
}
session.close();
}
}

Application will run, but need to close forcefully. This application creates a Service for SessionBuilder. Contact me if you have queries.

Advertisements

Spring And JSF with Hibernate Demo

Spring + Hibernate + JSF

Use following SQL Script to create table:


CREATE TABLE `products` (
`productId` int(8) NOT NULL,
`description` varchar(250) DEFAULT NULL,
`name` varchar(30) DEFAULT NULL,
`price` double DEFAULT NULL,
PRIMARY KEY (`productId`)
);

Sample Data :

| productId | description | name | price |
| 1 | Operating System for 128 Bit Systems | Mahendra OS 1.1 | 123000 |
| 2 | Office productivity Suite | Mahendra Office 2.0.1 | 63000 |
| 3 | IDE for Developing C,C++ and Java Applications | Mahendra IDE 2.3 | 543000 |

Frameworks Used:

Presentation Tier JSF 2.2
Business Tier Spring 3.1
Data Access Tier Hibernate 3

Dependencies [Actual JAR files]

  1. Hibernate 3

    • annotations-api.jar
    • cglib-2.2.jar
    • commons-collections.jar
    • commons-logging-1.1.1.jar
    • dom4j-1.6.1.jar
    • hibernate-jpa-2.0-api-1.0.0.Final.jar
    • hibernate3.jar
    • javassist-3.12.0.GA.jar
    • jta-1.1.jar
    • slf4j-api-1.6.1.jar
    • slf4j-simple-1.6.1.jar
  2. Spring 3

    • org.springframework.beans-3.1.0.M2.jar
    • org.springframework.context-3.1.0.M2.jar
    • org.springframework.context.support-3.1.0.M2.jar
    • org.springframework.core-3.1.0.M2.jar
    • org.springframework.expression-3.1.0.M2.jar
    • org.springframework.orm-3.1.0.M2.jar
    • org.springframework.transaction-3.1.0.M2.jar
    • org.springframework.web-3.1.0.M2.jar
    • org.springframework.jdbc-3.1.0.M2.jar
  3. Spring declarative Transactions

    • aopalliance-1.0.jar
    • asm-3.1.jar
    • org.springframework.aop-3.1.0.M2.jar
    • org.springframework.asm-3.1.0.M2.jar
  4. JSF 2.1

    • javax.faces-2.1.13.jar
    • jstl-1.2.jar
  5. JDBC Driver

    • mysql-connector-java-5.1.10-bin.jar

Create New Dynamic Web Project using eclipse with Apache Tomcat 6.0

Edit web.xml file to enable Spring

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Modify faces-config.xml to enable Spring EL inside JSF

<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>

Create hibernate.cfg.xml file inside ‘src’ folder

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mahendra</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/world</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.mahendra.models.Product"/>
</session-factory>
</hibernate-configuration>

Create applicationContext.xml inside ‘WEB-INF’ folder

<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
<tx:annotation-driven />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory” />
</bean>

<bean id=”dao” class=”com.mahendra.springbeans.ProductDAO”>
<property name=”template” ref=”ht” />
</bean>
</beans>

create: Product.java


package com.mahendra.models;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.*;

@ManagedBean @RequestScoped
@Entity @Table(name=”products”)
public class Product {

@Id private Integer productId;
private String name;
private String description;
private double price;
//generate getters and setters…
}

create : ProductDAO.java


package com.mahendra.springbeans;
import java.util.List;
import java.util.logging.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mahendra.models.Product;

@Service
public class ProductDAO {

private HibernateTemplate template;
private static Logger log = Logger.getLogger(ProductDAO.class.getName());
public HibernateTemplate getTemplate() {
return template;
}

public void setTemplate(HibernateTemplate template) {
this.template = template;
}

@Transactional
public void save(Product p){
log.info(“Preparing to save product “+p.getName());
template.save(p);

log.info(“Product “+p.getName()+ ” saved”);
}

@Transactional(readOnly=true)
public List findAll(){
List pl = null;
log.info(“Reading products from database”);
pl = template.loadAll(Product.class);
return pl;
}
}

Create ProductBean.java


package com.mahendra.jsfbeans;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import com.mahendra.models.Product;
import com.mahendra.springbeans.ProductDAO;
import java.util.logging.*;

@ManagedBean @ApplicationScoped
public class ProductBean {
private Product product;
private static Logger log = Logger.getLogger(ProductBean.class.getName());

private List productList;

@ManagedProperty(value=”#{dao}”)
private ProductDAO dao;

public Product getProduct() {
return product;
}

public void saveAction(){
log.info(“Saving product”);
dao.save(product);
listAction();
}

@PostConstruct
public void listAction(){
log.info(“Getting all products”);
setProductList(dao.findAll());
}

public void setProduct(Product product) {
this.product = product;
}

public List getProductList() {
return productList;
}

public void setProductList(List productList) {
this.productList = productList;
}

public ProductDAO getDao() {
return dao;
}

public void setDao(ProductDAO dao) {
this.dao = dao;
}
}

Create / Modify index.xhtml

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221;
xmlns:ui=”http://java.sun.com/jsf/facelets&#8221;
xmlns:h=”http://java.sun.com/jsf/html&#8221;
xmlns:f=”http://java.sun.com/jsf/core”&gt;
<head>
<title>Mahendra’s Spring + Hibernate + JSF Demo</title>
</head>
<body>
<h:form>
<h2>Add new product</h2>
<h:panelGrid columns=”2″>
<h:outputLabel value=”Product ID” for=”pid”/>
<h:inputText id=”pid” value=”#{product.productId}”/>
<h:outputLabel value=”Product Name” for=”pn”/>
<h:inputText id=”pn” value=”#{product.name}”/>
<h:outputLabel value=”Description” for=”pd”/>
<h:inputTextarea cols=”25″ rows=”3″ id=”pd” value=”#{product.description}”/>
<h:outputLabel value=”Price” for=”pc”/>
<h:inputText id=”pc” value=”#{product.price}”/>
<h:commandButton value=”Save” action=”#{productBean.saveAction}” >
<f:setPropertyActionListener value=”#{product}” target=”#{productBean.product}”/>
</h:commandButton>
</h:panelGrid>
</h:form>
<h2>List of Existing Products</h2>
<h:form>
<h:dataTable var=”p” value=”#{productBean.productList}”>
<h:column>
<f:facet name=”header”>Product ID</f:facet>
<h:outputText value=”#{p.productId}”/>
</h:column>
<h:column>
<f:facet name=”header”>Name</f:facet>
<h:outputText value=”#{p.name}”/>
</h:column>
<h:column>
<f:facet name=”header”>Description</f:facet>
<h:outputText value=”#{p.description}”/>
</h:column>
<h:column>
<f:facet name=”header”>Price</f:facet>
<h:outputText value=”#{p.price}”/>
</h:column>
</h:dataTable>
</h:form>
</body>
</html>

JSF sample app: part 3

JSF 2.0 Sample application – part3

in last post, I gave you DAO Components, now it’s time to explore JSF!

all JSF application requires one XML file …. faces-config.xml
I have stored this file under /WEB-INF folder.

<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">
<navigation-rule>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">listusers</span></from-outcome>
<to-view-id>/faces/users/list.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">listmovies</span></from-outcome>
<to-view-id>/faces/movies/list.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">listreviews</span></from-outcome>
<to-view-id>/faces/reviews/list.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">addreviews</span></from-outcome>
<to-view-id>/faces/reviews/add.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">listallreviews</span></from-outcome>
<to-view-id>/faces/reviews/listall.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">adduser</span></from-outcome>
<to-view-id>/faces/users/add.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/faces/login.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">edituser</span></from-outcome>
<to-view-id>/faces/users/edit.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome><span style="text-decoration:underline;">addmovie</span></from-outcome>
<to-view-id>/faces/movies/add.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>home</from-outcome>
<to-view-id>/default.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<locale-config>
<default-locale><span style="text-decoration:underline;">en</span></default-locale>
<supported-locale><span style="text-decoration:underline;">en</span></supported-locale>
<!--<span style="text-decoration:underline;">Uncomment</span> following line once translation is done
<supported-<span style="text-decoration:underline;">locale</span>><span style="text-decoration:underline;">mr</span></supported-<span style="text-decoration:underline;">locale</span>>
-->
</locale-config>
</application>
</faces-config>

Backing beans: package=com.mahendra.actions

AuthenticationBean:            responsible for User authentication (login) and logout events


import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

import javax.faces.context.FacesContext;

import com.mahendra.dao.UserDAO;

import com.mahendra.models.Users;

import com.mahendra.utils.MessageBox;

@ManagedBean

@SessionScoped

public class AuthenticationBean {

private UserDAO dao;

private String uname, upass;

private boolean loggedIn = false;

private Users user;

public AuthenticationBean() {

dao = new UserDAO();

}

public String login(){

return "login";

}

public String verifyUser(){

try{

user =  dao.getUser(getUname());

if(user!=null){

if(user.getPassword().equals(upass)){

loggedIn=true;

}

else

MessageBox.show("Invalid password");

}

}catch(RuntimeException ex){

MessageBox.show(ex.getMessage());

return null;

}

System.out.println("user[authentication]: "+user.getUsername());

if(!loggedIn)

return null;

return "home";

}

public String logout(){

loggedIn = false;

user = null;

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

System.out.println("Session destroyed!");

return "home";

}

public String signup(){

return "adduser";

}

public void setLoggedIn(boolean loggedIn) {

this.loggedIn = loggedIn;

}

public boolean isLoggedIn() {

return loggedIn;

}

public void setUpass(String upass) {

this.upass = upass;

}

public String getUpass() {

return upass;

}

public void setUname(String uname) {

this.uname = uname;

}

public String getUname() {

return uname;

}

public Users getUser() {

return user;

}

public void setUser(Users user) {

this.user = user;

}

}

MovieBean:              Beacking bean for all pages where movie details are either listed and edited


import java.util.List;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

import com.mahendra.dao.MovieDAO;

import com.mahendra.models.Movies;

@ManagedBean

@SessionScoped

public class MovieBean {

private MovieDAO dao;

private Movies movie;

private List<Movies> moviesList;

public void setMovie(Movies movie) {

this.movie = movie;

}

public Movies getMovie() {

return movie;

}

public MovieBean() {

dao = new MovieDAO();

}

public List<Movies> getMoviesList() {

return dao.getAllMovies();

}

public String add() {

setMovie(new Movies());

return "addmovie";

}

public String save() {

dao.create(getMovie());

return "listmovies";

}

public String list() {

return "listmovies";

}

public void setMoviesList(List<Movies> moviesList) {

this.moviesList = moviesList;

}

}

ReviewsBean: backing bean for all review pages:


import java.util.List;

import javax.faces.bean.*;

import javax.faces.component.html.HtmlDataTable;

import javax.faces.event.ActionEvent;

import com.mahendra.dao.ReviewsDAO;

import com.mahendra.models.Movies;

import com.mahendra.models.Reviews;

import com.mahendra.utils.MovieReviewCount;

@ManagedBean

@SessionScoped

public class ReviewsBean {

private ReviewsDAO dao;

private Reviews review;

@ManagedProperty(value = "#{movieBean.movie}")

private Movies movie;

@ManagedProperty(value = "#{authenticationBean}")

private AuthenticationBean authentication;

private List<MovieReviewCount> counts;

private List<Reviews> reviewList;

private HtmlDataTable topMovies;

public ReviewsBean() {

dao = new ReviewsDAO();

}

// public void getReviewsForMovie(){

// setReviewList(dao.getReviewsForMovie(movie.getMovieId()));

// }

//

// public void getAllReviews(){

// setReviewList(dao.getAll());

// }

public Movies getMovie() {

return movie;

}

public void setMovie(Movies movie) {

this.movie = movie;

}

// public List<Reviews> getReviewsForUser(){

// return dao.getReviewsByUser(user.getUserid());

// }

public String add() {

setReview(new Reviews());

return "addreviews";

}

public String save() {

// if(getUser()==null){

// MessageBox.show("Your session has expired!");

// return "login";

// }

review.setUser(getAuthentication().getUser());

review.setMovie(movie);

dao.create(review);

System.out.println("user : "

+ getAuthentication().getUser().getUsername());

return listall();

}

public String listall() {

setReviewList(dao.getAll());

return "listallreviews";

}

public String list() {

setReviewList(dao.getReviewsForMovie(movie.getMovieId()));

return "listreviews";

}

public void setReview(Reviews review) {

this.review = review;

}

public Reviews getReview() {

return review;

}

public void setReviewList(List<Reviews> reviewList) {

this.reviewList = reviewList;

}

public List<Reviews> getReviewList() {

return reviewList;

}

public List<MovieReviewCount> getCounts() {

List<MovieReviewCount> list = null;

list = dao.getReviewCount();

return list;

}

public void showAll(ActionEvent event) {

topMovies.setRows(0);

}

public void showTop(ActionEvent event) {

topMovies.setRows(5);

}

public void setAuthentication(AuthenticationBean authentication) {

this.authentication = authentication;

}

public AuthenticationBean getAuthentication() {

return authentication;

}

public void showMy() {

setReviewList(dao.getReviewsByUser(getAuthentication().getUser()

.getUserid()));

}

public void setTopMovies(HtmlDataTable topMovies) {

this.topMovies = topMovies;

}

public void showPublic() {

setReviewList(dao.getAll());

}

public HtmlDataTable getTopMovies() {

return topMovies;

}

}

UserBean: backing bean for User pages


import java.util.List;

import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.ManagedProperty;

import javax.faces.bean.SessionScoped;

import javax.faces.context.FacesContext;

import com.mahendra.dao.UserDAO;

import com.mahendra.models.Users;

@ManagedBean

@SessionScoped

public class UserBean {

private UserDAO dao = null;

private String oldpass,newpass1,newpass2;

@ManagedProperty(value="#{authenticationBean.user}")

private Users user = null;

private List<Users> userList;

public void setUser(Users user) {

this.user = user;

}

public Users getUser() {

return user;

}

public String addUser(){

setUser(new Users());

return "adduser";

}

public String editUser(){

return "edituser";

}

public String saveUser(){

dao.create(user.getUsername(), user.getPassword());

return "home";

}

public UserBean() {

dao = new UserDAO();

}

public String updateUser(){

if(getOldpass().equals(user.getPassword())){

if(getNewpass1().equals(getNewpass2())){

dao.changePassword(user.getUserid(), getNewpass1());

return "home";

}

else{

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("new password doesn't match!!"));

return "edituser";

}

}

else

{

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Invalid old password!"));

return "edituser";

}

}

public List<Users> getUserList() {

userList =dao.getAllUsers();

return userList;

}

public void setOldpass(String oldpass) {

this.oldpass = oldpass;

}

public String getOldpass() {

return oldpass;

}

public void setNewpass1(String newpass1) {

this.newpass1 = newpass1;

}

public String getNewpass1() {

return newpass1;

}

public void setNewpass2(String newpass2) {

this.newpass2 = newpass2;

}

public String getNewpass2() {

return newpass2;

}

}

Localization: this application is localized for english language, I am planning to add another language (Marathi). I will post it later….

you can save this in “src” folder or in new source folder in same eclipse project.

LocalizationResources_en.properties:


#messages

welcomeMessage=Welcome to Mahendra's Online Movie reviews

welcomeLine2=From this <span style="text-decoration: underline;">website</span> you can view / add reviews for movies

welcomeLine3=Adding reviews is available to only registered users

top5=Top 5 Movies (# of reviews received)

userRequired=Please enter <span style="text-decoration: underline;">username</span>

passRequired=Please enter password

homePage=Home Page

appTitle=Mahendra's Online Movie Reviews

showAllReviews=Showing ALL Reviews

noReviews=No Reviews found!

userPrompt=Enter <span style="text-decoration: underline;">Username</span>:

passPrompt=Enter Password:

loginTitle=Log In page

#links

linkHome=Home

linkAddMovie=Add New Movie

linkLogIn=Log In

linkLogOut=Log out

linkSignUp=Sign up

linkChangePass=Change Password

linkAllMovies=All Movies

linkAllReviews=All Reviews

linkTop5=Top 5 Only

linkAll=Show All

linkShowMy=Show MY reviews only

linkShowAll=Show ALL

#screenTips

tipShowAll=Show me ALL user's reviews

tipShowMy=Show me ONLY MY reviews

tipShow5=Show TOP 5 Only

#column headings

chTitle=Movie Title

chMessage=Message/Review

chCount=No of Reviews

chAverage=Average Ratings

chTime=Recorded at

chRatings=Ratings

chAuthor=Written By

chNo=Sr No

chTotal=Total Reviews Received

Your “web.xml” needs modification to support JSF


<?xml version=<em>"1.0"</em> encoding=<em>"UTF-8"</em>?>

<web-app xmlns:xsi=<em>"http://www.w3.org/2001/XMLSchema-instance"</em>

xmlns=<em>"http://java.sun.com/xml/ns/javaee"</em> xmlns:web=<em>"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</em>

xsi:schemaLocation=<em>"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</em>

id=<em>"WebApp_ID"</em> version=<em>"2.5"</em>>

<display-name>MovieReviews</display-name>

<welcome-file-list>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>Faces <span style="text-decoration: underline;">Servlet</span></servlet-name>

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

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

</servlet>

<servlet-mapping>

<servlet-name>Faces <span style="text-decoration: underline;">Servlet</span></servlet-name>

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

</servlet-mapping>

<session-config>

<session-timeout>15</session-timeout>

</session-config>

<error-page>

<error-code>404</error-code>

<location>/default.jsp</location>

</error-page>

<error-page>

<exception-type>javax.faces.application.ViewExpiredException</exception-type>

<location>/default.jsp</location>

</error-page>

</web-app>

Now, finally  xhtml pages to render VIEW (UI) for entire web-application.

the entire structure for pages (under “webContents” folder in eclipse project)

default.jsp:                               placeholder page. This is defined as “welcome-page” in web.xml.

It will redirect users to “index.xhtml”

<%@ page language=<em>"java"</em> contentType=<em>"text/html; charset=ISO-8859-1"</em>

pageEncoding=<em>"ISO-8859-1"</em>%>

<%@taglib uri=<em>"http://java.sun.com/jsp/jstl/core"</em>  prefix=<em>"c"</em>%>

<c:redirect url=<em>"/faces/index.xhtml"</em>/>

template.xhtml:     JSF template page to create Common layout


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em>

xmlns:h=<em>"http://java.sun.com/jsf/html"</em>

xmlns:f=<em>"http://java.sun.com/jsf/core"</em>

xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<head>

<f:loadBundle var=<em>"msg"</em> basename=<em>"LocalizationResources"</em> />

<meta http-equiv=<em>"Content-Type"</em> content=<em>"text/html; charset=ISO-8859-1"</em> />

<link type=<em>"text/css"</em> rel=<em>"stylesheet"</em>

href=<em>"#{facesContext.externalContext.requestContextPath}/res/main-style.css"</em> />

<title>#{msg.appTitle} <ui:insert name=<em>"title"</em>>#{msg.homePage}</ui:insert>

</title>

</head>

<body>

<table border=<em>"1"</em> cellspacing=<em>"0"</em> cellpadding=<em>"5"</em> width=<em>"100%"</em>>

<tr <span style="text-decoration: underline;">height</span>=<em>"120px;"</em> bgcolor=<em>"#efd5ab"</em>>

<td><h:form>

<h1>#{msg.welcomeMessage}</h1>

<div style="float: <em>right</em>">

<h:outputText value=<em>"Welcome #{authenticationBean.user.username}"</em>

rendered=<em>"#{authenticationBean.loggedIn}"</em> />

</div>

<h:commandLink value=<em>"#{msg.linkHome}"</em> action=<em>"home"</em> />

<h:commandLink value=<em>"#{msg.linkAllMovies}"</em>

action=<em>"#{movieBean.list}"</em> />

<h:commandLink value=<em>"#{msg.linkAllReviews}"</em>

action=<em>"#{reviewsBean.listall}"</em> />

<h:commandLink value=<em>"#{msg.linkLogIn}"</em>

action=<em>"#{authenticationBean.login}"</em>

rendered=<em>"#{!authenticationBean.loggedIn}"</em> />

<h:commandLink value=<em>"#{msg.linkLogOut}"</em>

action=<em>"#{authenticationBean.logout}"</em>

rendered=<em>"#{authenticationBean.loggedIn}"</em> />

<h:commandLink value=<em>"#{msg.linkChangePass}"</em>

action=<em>"#{userBean.editUser}"</em>

rendered=<em>"#{authenticationBean.loggedIn}"</em> />

</h:form></td>

</tr>

<tr <span style="text-decoration: underline;">height</span>=<em>"400px"</em> valign=<em>"top"</em>>

<td><ui:insert name=<em>"content"</em>>Contents for page</ui:insert></td>

</tr>

<tr bgcolor=<em>"#c5dde6"</em>>

<td><ui:insert name=<em>"footer"</em>>NO Copyrights &copy; 2011 : <span style="text-decoration: underline;">Mahendra</span> <span style="text-decoration: underline;">Shinde</span></ui:insert>

</td>

</tr>

</table>

</body>

</html>

index.xhtml:             The REAL homepage


<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em>

xmlns:h=<em>"http://java.sun.com/jsf/html"</em>

xmlns:f=<em>"http://java.sun.com/jsf/core"</em>

xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>#{msg.homePage}</ui:define>

<ui:define name=<em>"content"</em>>

#{msg.welcomeMessage}<br />

#{msg.welcomeLine2}<br />

<b><i>#{msg.welcomeLine3}</i>

</b>

<h3>#{msg.top5}</h3>

<h:dataTable value=<em>"#{reviewsBean.counts}"</em> var=<em>"m"</em> rows=<em>"5"</em>

styleClass=<em>"dtable"</em> binding=<em>"#{reviewsBean.topMovies}"</em>

rowClasses=<em>"odd,even"</em>>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chTitle}"</em> />

</f:facet>

<h:outputText value=<em>"#{m.title}"</em> />

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chTotal}"</em> />

</f:facet>

<h:outputText value=<em>"#{m.count}"</em> />

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chAverage}"</em> />

</f:facet>

<h:outputText value=<em>"#{m.average}"</em> />

</h:column>

</h:dataTable>

<h:form>

<h:commandLink accesskey=<em>"A"</em> value=<em>"#{msg.linkShowAll}"</em>

actionListener=<em>"#{reviewsBean.showAll}"</em> title=<em>"#{msg.tipShowAll}"</em> />

<h:commandLink accesskey=<em>"t"</em> value=<em>"#{msg.linkTop5}"</em>

actionListener=<em>"#{reviewsBean.showTop}"</em> title=<em>"#{msg.tipShow5}"</em> />

</h:form>

<h:outputText value=<em>"#{msg.noReviews}"</em>

rendered=<em>"#{empty reviewsBean.counts}"</em> />

</ui:define>

</ui:composition>

</html>

login.xhtml:              page for log-in


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em>

xmlns:h=<em>"http://java.sun.com/jsf/html"</em>

xmlns:f=<em>"http://java.sun.com/jsf/core"</em>

xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>#{msg.loginTitle}</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h:messages />

<h:panelGrid columns=<em>"2"</em>>

<h:outputText value=<em>"#{msg.userPrompt}"</em> />

<h:inputText value=<em>"#{authenticationBean.uname}"</em> required=<em>"true"</em>

requiredMessage=<em>"username can not be blank!"</em> />

<h:outputText value=<em>"#{msg.passPrompt}"</em> />

<h:inputSecret value=<em>"#{authenticationBean.upass}"</em> required=<em>"true"</em>

requiredMessage=<em>"password cannot be blank!"</em> />

<h:commandButton value=<em>"#{msg.linkLogIn}"</em>

action=<em>"#{authenticationBean.verifyUser}"</em> />

</h:panelGrid>

</h:form>

</ui:define>

</ui:composition>

</html>

users/add.xhtml:   page to add new user (signup)


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em>

xmlns:h=<em>"http://java.sun.com/jsf/html"</em>

xmlns:f=<em>"http://java.sun.com/jsf/core"</em>

xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>User Registration</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h:panelGrid columns=<em>"3"</em>>

<h:outputText value=<em>"Enter username: "</em> />

<h:inputText value=<em>"#{userBean.user.username}"</em> id=<em>"uname"</em>

required=<em>"true"</em> requiredMessage=<em>"*"</em> />

<h:message for=<em>"uname"</em> />

<h:outputText value=<em>"Enter password: "</em> />

<h:inputSecret value=<em>"#{userBean.user.password}"</em> id=<em>"upass"</em>

required=<em>"true"</em> requiredMessage=<em>"*"</em> />

<h:message for=<em>"upass"</em> />

<h:commandButton value=<em>"Save"</em> type=<em>"submit"</em>

action=<em>"#{userBean.saveUser}"</em> />

<h:commandButton value=<em>"Reset"</em> type=<em>"reset"</em> />

</h:panelGrid>

</h:form>

</ui:define>

</ui:composition>

</html>

users/edit.xhtml:   page to change password


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em>

xmlns:h=<em>"http://java.sun.com/jsf/html"</em>

xmlns:f=<em>"http://java.sun.com/jsf/core"</em>

xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>Change Password</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h:panelGrid columns=<em>"2"</em>>

<h:outputText value=<em>"Enter old password: "</em> />

<h:inputSecret value=<em>"#{userBean.oldpass}"</em> required=<em>"true"</em>

requiredMessage=<em>"Old password is missing!"</em> id=<em>"oldpass"</em> />

<h:outputText value=<em>"Enter new password: "</em> />

<h:inputSecret value=<em>"#{userBean.newpass1}"</em> id=<em>"newpass1"</em>

required=<em>"true"</em> requiredMessage=<em>"New password is missing!"</em> />

<h:outputText value=<em>"Repeate new password: "</em> />

<h:inputSecret value=<em>"#{userBean.newpass2}"</em> id=<em>"newpass2"</em>

required=<em>"true"</em> requiredMessage=<em>"Re-enter new password"</em>>

</h:inputSecret>

<h:commandButton value=<em>"change"</em> type=<em>"submit"</em>

action=<em>"#{userBean.updateUser}"</em> />

<h:messages showSummary=<em>"true"</em> />

</h:panelGrid>

</h:form>

</ui:define>

</ui:composition>

</html>

reviews/add.xhtml


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em>

xmlns:h=<em>"http://java.sun.com/jsf/html"</em>

xmlns:f=<em>"http://java.sun.com/jsf/core"</em>

xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>Add new review</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>Adding

new review for #{reviewsBean.movie.title} <h:panelGrid columns=<em>"3"</em>>

<h:outputText value=<em>"Enter Message: "</em> />

<h:inputTextarea rows=<em>"5"</em> cols=<em>"35" </em>                        value=<em>"#{reviewsBean.review.message}"</em> id=<em>"message"</em> required=<em>"true"</em>

requiredMessage=<em>"*"</em> />

<h:message for=<em>"message"</em> />

<h:outputText value=<em>"Select Ratings: "</em> />

<h:selectOneRadio id=<em>"ratings"</em> required=<em>"true"</em> requiredMessage=<em>"*"</em>

layout=<em>"lineDirection"</em> value=<em>"#{reviewsBean.review.ratings}"</em>>

<f:selectItem itemValue=<em>"1"</em> itemLabel=<em>"*"</em> />

<f:selectItem itemValue=<em>"2"</em> itemLabel=<em>"**"</em> />

<f:selectItem itemValue=<em>"3"</em> itemLabel=<em>"***"</em> />

<f:selectItem itemValue=<em>"4"</em> itemLabel=<em>"****"</em> />

<f:selectItem itemValue=<em>"5"</em> itemLabel=<em>"*****"</em> />

</h:selectOneRadio>

<h:message for=<em>"ratings"</em> />

<h:commandButton value=<em>"Save"</em> type=<em>"submit"</em>

action=<em>"#{reviewsBean.save}"</em>

rendered=<em>"#{authenticationBean.loggedIn}"</em> />

<h:commandButton value=<em>"Reset"</em> type=<em>"reset"</em> />

</h:panelGrid>

</h:form>

</ui:define>

</ui:composition>

</html>

reviews/list.xhtml  : list of reviews for selected movie


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em> xmlns:h=<em>"http://java.sun.com/jsf/html"</em> xmlns:f=<em>"http://java.sun.com/jsf/core"</em> xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>Reviews for #{reviewsBean.movie.title}</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h3>Showing  reviews for <b>#{reviewsBean.movie.title}(#{reviewsBean.movie.realsedIn})</b> directed by <i>#{reviewsBean.movie.director}</i></h3>

<h:dataTable id=<em>"data1"</em> value=<em>"#{reviewsBean.reviewList}"</em> var=<em>"review"</em> styleClass=<em>"dtable"</em> rowClasses=<em>"odd,even"</em>>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"Sr. No."</em>/>

</f:facet>

<h:outputText value=<em>"#{review.reviewId}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"user"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.user.username}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"ratings"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.ratings}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"message"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.message}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"time"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.recordtime}"</em>/>

</h:column>

</h:dataTable>

<h:commandLink value=<em>"new review"</em> action=<em>"#{reviewsBean.add}"</em> rendered=<em>"#{authenticationBean.loggedIn}"</em>>

</h:commandLink>

<h:outputText value=<em>"Be FIRST to review!"</em> rendered=<em>"#{empty reviewsBean.reviewList}"</em>/>

</h:form>

</ui:define>

</ui:composition>

</html>

reviews/listall.xhtml             list of all reviews


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em> xmlns:h=<em>"http://java.sun.com/jsf/html"</em> xmlns:f=<em>"http://java.sun.com/jsf/core"</em> xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>#{msg.showAllReviews}</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h3>#{msg.showAllReviews} <i></i></h3>

<h:dataTable id=<em>"data1"</em> value=<em>"#{reviewsBean.reviewList}"</em> var=<em>"review"</em> styleClass=<em>"dtable"</em> rowClasses=<em>"odd,even"</em>>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chNo}"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.reviewId}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chTitle}"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.movie.title}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chAuthor}"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.user.username}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chRatings}"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.ratings}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"#{msg.chMessage}"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.message}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"time"</em>/>

</f:facet>

<h:outputText value=<em>"#{review.recordtime}"</em>>

<f:convertDateTime type=<em>"both"</em> timeStyle=<em>"medium"</em> timeZone=<em>"IST"</em>/>

</h:outputText>

</h:column>

</h:dataTable>

<h:panelGroup rendered=<em>"#{authenticationBean.loggedIn}"</em>>

<h:commandLink  accesskey=<em>"A"</em> value=<em>"#{msg.linkAll}"</em> actionListener=<em>"#{reviewsBean.showPublic}"</em> title=<em>"#{msg.tipShowAll}"</em>/>

<h:commandLink accesskey=<em>"M"</em> value=<em>"#{msg.linkShowMy}"</em> actionListener=<em>"#{reviewsBean.showMy}"</em> title=<em>"#{msg.tipShowMy}"</em>/>

</h:panelGroup>

</h:form>

</ui:define>

</ui:composition>

</html>

movies/add.xhtml page to add new movie


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em> xmlns:h=<em>"http://java.sun.com/jsf/html"</em> xmlns:f=<em>"http://java.sun.com/jsf/core"</em> xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>Add new movie</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h:panelGrid columns=<em>"3"</em>>

<h:outputText value=<em>"Enter title: "</em>/>

<h:inputText value=<em>"#{movieBean.movie.title}"</em> id=<em>"title"</em> required=<em>"true"</em> requiredMessage=<em>"*"</em>/>

<h:message for=<em>"title"</em>/>

<h:outputText value=<em>"Enter director: "</em>/>

<h:inputText value=<em>"#{movieBean.movie.director}"</em> id=<em>"director"</em> required=<em>"true"</em> requiredMessage=<em>"*"</em>/>

<h:message for=<em>"director"</em>/>

<h:outputText value=<em>"Select Category: "</em>/>

<h:selectOneRadio id=<em>"category"</em> required=<em>"true"</em> requiredMessage=<em>"*"</em> layout=<em>"pageDirection"</em> value=<em>"#{movieBean.movie.category}"</em>>

<f:selectItem itemValue=<em>"comedy"</em> itemLabel=<em>"Comedy"</em>/>

<f:selectItem itemValue=<em>"action"</em> itemLabel=<em>"Action"</em>/>

<f:selectItem itemValue=<em>"comedy-action"</em> itemLabel=<em>"Comedy with action"</em>/>

<f:selectItem itemValue=<em>"sci-fi"</em> itemLabel=<em>"Science Fictions"</em>/>

<f:selectItem itemValue=<em>"thriller"</em> itemLabel=<em>"Thrillers"</em>/>

<f:selectItem itemValue=<em>"horror"</em> itemLabel=<em>"Horror"</em>/>

</h:selectOneRadio>

<h:message for=<em>"category"</em>/>

<h:outputText value=<em>"Enter length (minutes): "</em>/>

<h:inputText value=<em>"#{movieBean.movie.duration}"</em> id=<em>"length"</em> required=<em>"true"</em> requiredMessage=<em>"*"</em>/>

<h:message for=<em>"length"</em>/>

<h:outputText value=<em>"Enter year of release: "</em>/>

<h:inputText value=<em>"#{movieBean.movie.realsedIn}"</em> id=<em>"ryear"</em> required=<em>"true"</em> requiredMessage=<em>"*"</em>/>

<h:message for=<em>"ryear"</em>/>

<h:commandButton value=<em>"Save"</em> type=<em>"submit"</em> action=<em>"#{movieBean.save}"</em> rendered=<em>"#{authenticationBean.loggedIn}"</em>/>

<h:commandButton value=<em>"Reset"</em> type=<em>"reset"</em>/>

</h:panelGrid>

</h:form>

</ui:define>

</ui:composition>

</html>

movies/list.xhtml   page to list all movies


<?xml version=<em>"1.0"</em> encoding=<em>"ISO-8859-1"</em> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=<em>"http://www.w3.org/1999/xhtml"</em> xmlns:h=<em>"http://java.sun.com/jsf/html"</em> xmlns:f=<em>"http://java.sun.com/jsf/core"</em> xmlns:ui=<em>"http://java.sun.com/jsf/facelets"</em>>

<ui:composition template=<em>"/WEB-INF/layout/template.xhtml"</em>>

<ui:define name=<em>"title"</em>>List of Movies</ui:define>

<ui:define name=<em>"content"</em>>

<h:form>

<h:dataTable id=<em>"data1"</em> value=<em>"#{movieBean.moviesList}"</em> var=<em>"movie"</em> styleClass=<em>"dtable"</em> rowClasses=<em>"odd,even"</em>>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"Sr. No."</em>/>

</f:facet>

<h:outputText value=<em>"#{movie.movieId}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"TITLE"</em>/>

</f:facet>

<h:outputText value=<em>"#{movie.title}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"DIRECTOR"</em>/>

</f:facet>

<h:outputText value=<em>"#{movie.director}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"CATEGORY"</em>/>

</f:facet>

<h:outputText value=<em>"#{movie.category}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"LENGTH"</em>/>

</f:facet>

<h:outputText value=<em>"#{movie.duration}"</em>/>

</h:column>

<h:column>

<f:facet name=<em>"header"</em>>

<h:outputText value=<em>"Reviews"</em>/>

</f:facet>

<h:commandLink value=<em>"read reviews"</em> action=<em>"#{reviewsBean.list}"</em>>

<f:setPropertyActionListener target=<em>"#{reviewsBean.movie}"</em> value=<em>"#{movie}"</em>/>

</h:commandLink>

</h:column>

</h:dataTable>

<h:commandLink value=<em>"Add movie"</em> action=<em>"#{movieBean.add}"</em> rendered=<em>"#{authenticationBean.loggedIn}"</em>></h:commandLink>

</h:form>

</ui:define>

</ui:composition>

</html>

res/main-style.css  CSS stylesheet for entire application


@CHARSET <em>"ISO-8859-1"</em>;

<strong>body</strong>

{

margin:<em>10px</em>;

background-color: <em>#ddeedd</em>;

}

<strong>a</strong>

{

display: <em>inline-block</em>;

padding:<em>4px</em>;

color:<em>#ff0033</em>;

font-family: <em>Tahoma</em>;

font-size: <em>11pt</em>;

}

<strong>a</strong><em>:hover</em>{

background-color: <em>#793900</em>;

color:<em>white</em>;

text-decoration: <em>none</em>;

}

<em>.dtable</em>

{

font-family: <em>Arial</em>;

font-size: <em>11pt</em>;

padding: <em>5px</em>;

cellpadding:<em>10px</em>;

border:<em>2px</em> <em>solid</em> <em>black</em>;

}

<em>.dtable</em> <strong>thead</strong>

{

background-color: <em>#cc9c6c</em>;

color:<em>#770000</em>;

}

<em>.dtable</em> <em>.odd</em>

{

background-color: <em>#fedea3</em>;

border-bottom-color: <em>darkred</em>;

}

<em>.dtable</em> <strong>tbody</strong> <strong>tr</strong> <strong>td</strong> {

border-top: <em>1px</em> <em>solid</em> <em>#cc9c6c</em>;

border-bottom: <em>1px</em> <em>solid</em> <em>#cc9c6c</em>;

}

<em>.dtable</em> <em>.even</em>

{

background-color: <em>#fede49</em>;

border-bottom: <em>1px</em> <em>solid</em> <em>darkred</em>;

}

<em>.dtable</em> <strong>th,td</strong>

{

padding: <em>5px</em>;

}

<em>.dtable</em> <strong>tbody</strong> <strong>tr</strong><em>:hover</em> <strong>td</strong>

{

border-top: <em>0px</em> <em>solid</em> <em>darkred</em>;

background-color:<em>#9fcc86</em>;

border-bottom: <em>2px</em> <em>solid</em> <em>darkred</em>;

}

<strong>h1,h2,h3</strong>

{

font-family: <em>Tahoma</em>;

text-shadow: <em>2px</em> <em>3px</em> <em>orange</em>;

}

JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2

JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2

in last post, you have seen all model classes, hibernate configuration and mappings.

in this post I will show you Data Access Objects (DAO components: data access layer)

for each Model class, I have created a separate DAO class:

com.mahendra.dao.MovieDAO


package com.mahendra.dao;
import java.util.List;
import org.hibernate.*;
import com.mahendra.models.Movies;
import com.mahendra.utils.HibernateUtils;

public class MovieDAO {

SessionFactory fact = null;

public MovieDAO() {
fact = HibernateUtils.getSessionFactory();
}

public void create(Movies m) {

Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
session.save(m);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

}

public Movies getMovie(String title) {
Session session = fact.getCurrentSession();
Transaction t = null;
Movies m = null;
try {
t = session.beginTransaction();
m = (Movies) session.createQuery("from Movies where title=:title")
.setString("title", title).list().get(0);
t.commit();
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}

return m;
}

public Movies getMovie(int id) {
Session session = fact.getCurrentSession();
Transaction t = null;
Movies m = null;
try {
t = session.beginTransaction();
m = (Movies) session.get(Movies.class, id);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return m;
}

public List<Movies> getAllMovies() {
List<Movies> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
list = session.createQuery("from Movies").list();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

}

com.mahendra.dao.ReviewsDAO:

package com.mahendra.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.*;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import com.mahendra.models.Movies;
import com.mahendra.models.Reviews;
import com.mahendra.utils.HibernateUtils;
import com.mahendra.utils.MovieReviewCount;

public class ReviewsDAO {

private SessionFactory fact;

public ReviewsDAO() {
fact = HibernateUtils.getSessionFactory();
}

public void create(Reviews review) {
Session session = fact.getCurrentSession();

Transaction t = null;
try {
t = session.beginTransaction();
session.save(review);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}
}

@SuppressWarnings("unchecked")

public List<Reviews> getReviewsForMovie(int id) {
List<Reviews> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;

try {
t = session.beginTransaction();
list = session
   .createQuery("from Reviews where movie.movieId=:movieid")
   .setInteger("movieid", id).list();
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

public Reviews getReview(int id) {
Session session = fact.getCurrentSession();
Reviews review = null;
Transaction t = null;
try {
t = session.beginTransaction();
review = (Reviews) session.get(Reviews.class, id);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return review;
}

public List<Reviews> getReviewsByUser(int userid) {
List<Reviews> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
list = session.createQuery("from Reviews where userid = :userid")
.setInteger("userid", userid).list();
t.commit();
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

public List<Reviews> getAll() {
List<Reviews> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
list = session.createQuery("from Reviews").list();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

public Movies getTopper() {
Movies m = null;
Session session = fact.getCurrentSession();

Transaction t = null;
try {
t = session.beginTransaction();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();

}

return m;
}

public List<MovieReviewCount> getReviewCount() {

List<MovieReviewCount> list = new ArrayList<MovieReviewCount>();
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
SQLQuery q = session
.createSQLQuery("select m.title as title,count(r.message) as count, avg(r.ratings) as average  from app.movies m join app.reviews"
+ " r on (m.movie_id=r.movie_id) group by m.title"
+ " order by count(r.message) desc");
q.addScalar("title")
.addScalar("count")
.addScalar("average")
.setResultTransformer(
Transformers.aliasToBean(MovieReviewCount.class));
list = q.list();
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();
System.out.println("Error: ");
he.printStackTrace();
}

return list;
}

}

com.mahendra.dao.UserDAO:


package com.mahendra.dao;

import java.util.List;
import org.hibernate.*;
import com.mahendra.models.Users;
import com.mahendra.utils.HibernateUtils;

public class UserDAO {

SessionFactory fact = null;

public UserDAO() {
fact = HibernateUtils.getSessionFactory();
}

public void create(String username, String password) {

Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
Users user = new Users();
user.setUsername(username);
user.setPassword(password);
Object obj = session.save(user);
System.out.println(obj);
t.commit();
System.out.println("record!");
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}
}

public List<Users> getAllUsers() {

Session session = fact.getCurrentSession();
Transaction t = null;
List<Users> users = null;
try {
t = session.beginTransaction();
users = (List<Users>) session.createQuery("from Users").list();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
System.out.println("Error: " + he.getMessage());
}

return users;

}

public void delete(int userid) {
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
Users user = (Users) session.get(Users.class, userid);
if (user == null)
throw new HibernateException("Record not found!");
session.delete(user);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
System.out.println("Error: " + he.getMessage());
}

}

public Users getUser(String username) {

Session session = fact.getCurrentSession();
Transaction t = null;
Users user = null;
try {
t = session.beginTransaction();
Query q = session.createQuery("from Users where username = :uname");
q.setString("uname", username);
List<Users> list = q.list();
if (list == null || list.isEmpty())
throw new RuntimeException("user '" + username
+ "' doesnot exists!");
user = list.get(0);
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();

}

return user;
}

public void changePassword(int userid, String newpass) {

Session session = fact.getCurrentSession();
Transaction t = null;
try {

t = session.beginTransaction();
Users user = (Users) session.get(Users.class, userid);
if (user == null)
throw new HibernateException("Record not found!");

user.setPassword(newpass);
session.update(user);
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();

System.out.println("Error: " + he.getMessage());
}

}

public Users getUser(int userid) {

Session session = fact.getCurrentSession();
Transaction t = null;
Users user = null;
try {
t = session.beginTransaction();
user = (Users) session.get(Users.class, userid);
if (user == null)
throw new HibernateException("Record not found!");
t.commit();
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}

return user;
}

}

next post… JSF configaration & navigation

Simple Blog type web-application using Struts+Hibernate+MySql

  1. Create table in database “mahendra”

create table blogEntry

(

blogid int primary key auto_increment,

username varchar(35)not null,

emailid varchar(60)not null,

message varchar(1000),

edate timestamp default now()

);

  1. Create new Dynamic Web Application using eclipse.
  2. Add required libraries into “lib” folder.
  3. Modify “web.xml” fileà add following lines:

<filter>

<filter-name>mahendra</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>mahendra</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  1. Create new JSP page inside “WebContent”à index.jsp
  2. Run your application for testing
  3. Create new “Source Folder” with name : “resources”
  4. Add new File with name: log4j.properties with following contents:

log4j.rootLogger = DEBUG, CA, FA

log4j.appender.CA = org.apache.log4j.ConsoleAppender

log4j.appender.CA.layout = org.apache.log4j.PatternLayout

log4j.appender.CA.ConversionPattern=%-5p %c %x {%m} %n

log4j.appender.FA = org.apache.log4j.FileAppender

log4j.appender.FA.layout = org.apache.log4j.PatternLayout

log4j.appender.FA.File=./sample.log

log4j.appender.FA.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n

  1. Add new XML file with name “struts.xml” with following contents

<!DOCTYPE struts PUBLIC

“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”

http://struts.apache.org/dtds/struts-2.0.dtd&#8221;

>

<struts>

<package name=“default” extends=“struts-default” namespace=“”>

<action name=“review” class=“com.mahendra.ReviewsAction” method=“list”>

<result>/WEB-INF/pages/reviews.jsp</result>

<result name=“error”>/WEB-INF/pages/reviews.jsp </result>

</action>

<action name=“add_review” class=“com.mahendra.ReviewsAction” method=“add”>

<result name=“success” type=“chain”>review</result>

<result name=“error”>/WEB-INF/pages/reviews.jsp </result>

</action>

</package>

</struts>

  1. Create new class called “Reviews” (Model component)

package com.mahendra;

import java.sql.Timestamp;

import javax.persistence.*;

@Entity

@Table(name=”blogentry”)

public class Reviews implements java.io.Serializable{

private static final long serialVersionUID = 1L;

private int blogid;

private String username;

private String emailid;

private String message;

private Timestamp edate;

@Id

@GeneratedValue

public int getBlogid() {

return blogid;

}

public void setBlogid(int blogid) {

this.blogid = blogid;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getEmailid() {

return emailid;

}

public void setEmailid(String emailid) {

this.emailid = emailid;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public Timestamp getEdate() {

return edate;

}

public void setEdate(Timestamp edate) {

this.edate = edate;

}

public Reviews(int blogid, String username, String emailid, String message

) {

super();

this.blogid = blogid;

this.username = username;

this.emailid = emailid;

this.message = message;

}

public Reviews() {

super();

}

}

  1. Create new class called “HibernateUtils” :

package com.mahendra;

import org.hibernate.*;

import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtils {

private static SessionFactory sessionFactory = buildFactory();

private static SessionFactory buildFactory(){

try{

return new AnnotationConfiguration().configure().buildSessionFactory();

}catch(Exception ex){

throw new ExceptionInInitializerError(“Unable to initialize session factory”);

}

}

public static SessionFactory getSessionFactory(){

return sessionFactory;

}

public static Session getSession()

{

return sessionFactory.getCurrentSession();

}

}

  1. Create new class “ReviewsManager”

package com.mahendra;

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.*;

public class ReviewsManager {

@SuppressWarnings(“unchecked”)

public List<Reviews> getAllReviews(){

Session session = HibernateUtils.getSession();

List<Reviews> list = null;

Transaction tran = null;

try{

tran = session.beginTransaction();

list = (List<Reviews>)session.createQuery(“from Reviews”).list();

tran.commit();

}catch(HibernateException he){

tran.rollback();

throw new MyException(“Unable to retrieve list of Reviews”,he.getMessage());

}

return list;

}

public void addReview(Reviews review){

Session session = HibernateUtils.getSession();

Transaction tran = null;

try{

tran = session.beginTransaction();

session.save(review);

tran.commit();

}catch(HibernateException he){

throw new MyException(“Unable to add your review.”,he.getMessage());

}

}

}

  1. Create new Exception with name “MyException”

package com.mahendra;

public class MyException extends RuntimeException {

String technicalDetails;

public MyException() {

// TODO Auto-generated constructor stub

}

public MyException(String arg0) {

super(arg0);

// TODO Auto-generated constructor stub

}

public MyException(String arg0, String arg1) {

super(arg0);

technicalDetails = arg1;

}

public String getTechnicalDetails(){

return technicalDetails;

}

}

  1. Create “ReviewAction” (Action compoenet)

package com.mahendra;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class ReviewsAction extends ActionSupport{

private static final long serialVersionUID = 1L;

private ReviewsManager rman = null;

private Reviews review = null;

private List<Reviews> reviewList = null;

public ReviewsAction(){

rman = new ReviewsManager();

}

public String list()

{

try{

reviewList = rman.getAllReviews();

return “success”;

}catch(ExceptionInInitializerError ex){

addActionMessage(“Could not connect with database”);

addActionError(“ERROR: “+ex.getMessage());

return “error”;

}catch(MyException me){

addActionMessage(me.getMessage());

addActionError(me.getTechnicalDetails());

return “error”;

}

}

public String add(){

try{

rman.addReview(getReview());

return “success”;

}catch(MyException me){

addActionMessage(me.getMessage());

addActionError(me.getTechnicalDetails());

return “error”;

}

}

public Reviews getReview() {

return review;

}

public void setReview(Reviews review) {

this.review = review;

}

public List<Reviews> getReviewList() {

return reviewList;

}

public void setReviewList(List<Reviews> reviewList) {

this.reviewList = reviewList;

}

}

  1. Create new JSP page :  /WEB-INF/pages/reviews.jsp

<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”

pageEncoding=“ISO-8859-1”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;

<%@taglib uri=http://java.sun.com/jsp/jstl/core&#8221;  prefix=“c”%>

<%@taglib uri=“/struts-tags”  prefix=“s”%>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>

<title>Insert title here</title>

</head>

<body>

list of reviews

<c:if test=${reviewList != null }>

<c:forEach items=${reviewList} var=“review”>

<table border=“1” width=“300” align=“right”>

<tr>

<td width=“100”>${review.blogid}</td>

<td>by ${review.username}</td>

<td>EMail: ${review.emailid}</td>

</tr>

<tr>

<td>Message: </td>

<td colspan=“2”>

${review.message }

</td>

</tr>

<tr>

<td colspan=“3” align=“right”>

recorder at : ${review.edate}

</td>

</tr>

</table> <br/>

</c:forEach>

</c:if>

<c:if test=“{reviewList is empty}”>

No reviews found!<br/>

Be First to add review..!

</c:if>

<s:form action=“add_review” method=“POST”>

<s:textfield name=“review.username” label=“Your name: “>

</s:textfield><br/>

<s:textfield name=“review.emailid” label=“Your Email ID:”></s:textfield><br/>

<s:textarea name=“review.message” label=“Your views:” rows=“4” cols=“40”>

</s:textarea><br/>

<s:submit value=“Submit”></s:submit>

</s:form>

<s:actionmessage/>

<font face=“Tahoma” size=“2” color=“red”>

<s:actionerror/>

</font>

</body>

</html>

  1. Create new JSP page index.jsp

<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”

pageEncoding=“ISO-8859-1”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>

<title>Home page: Mahendra’s Blog</title>

</head>

<body>

<h2>Some topic…..</h2>

<p>

Some details here…..

</p>

<a href=“reviews”>View / Add Reviews</a>

</body>

</html>