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>

JPA with JSF and Session Bean on Apache TomEE

Even though I am going to use Apache TomEE (Tomcat 7 +OpenEJB + OpenJPA), It will work on ANY JAVA EE 6 (Web Profile) Compatible Application server.

In this example I am trying to demonstrate how to use Apache TomEE for developing an Application made with JPA + EJB + JSF.

This demo application uses Apache derby (JavaDB) database which comes free of cost with JDK 7 downloads. You may use any other database with tiny changes to some configuration files.

Pre-requisites:

Preparations:

  1. Create Database [You may skip this if you are using any other database]
  2. Open Terminal / MS DOS prompt
  3. goto JAVA_HOME (on windows: C:\Program Files\Java\jdk1.7.x_xx)
  4. goto db\bin
  5. use command:    startNetworkServer
  6. open eclipse → create new workspace → using Data source Explorer connect to Derby connection properties:
    url: jdbc:derby://localhost:1527/sample;create=true
    username: app
    password: app
  7. Open SQL Scrapbook → execute following queries:
    create table Contact(
    contactId int primary key generated always as identity,
    first_name varchar(25),
    last_name varchar(35),
    phone_no varchar(15)
    );
    insert into contact(first_name,last_name,phone_no)
    values('Katrina','Kaif','1020131');
    insert into contact(first_name,last_name,phone_no)
    values('Kareena','Kapoor','10480131');
    insert into contact(first_name,last_name,phone_no)
    values('Prachi','desai','1045431');
  8. Add server to current workspace [You may skip this if you know how to do it]
    1. Open “Servers” view → Right click → New → Server → Apache: Apache Tomcat 7
    2. Click on “Browse” and provide path to your TomEE folder → Finish
    3. Double click on Apache Tomcat 7 inside “Servers” view
    4. Change “Server Locations” to “Use Tomcat Installation”

    [This must be done when BEFORE ANY PROJECT IS ADDED TO TOMCAT]

  9. Create New Dynamic Web Project
    1. New–> Dynamic Web Project → Name: TestApp2
    2. Click on “Modify” to change Configuration
    3. Select “JPA” and “JSF” project facets

    [ One Best thing about TomEE, it comes bundled with OpenJPA and JSF so forget about downloading and copying required JARs into your application’s WEB-INF/lib]

    • Create JNDI DataSource [MUST READ]
    • Stop Apache TomEE (If already started)
    • Open TomEE’s “conf” folder → Open “tomee.xml” in any text editor
    • add following resource definition:
      <Resource id="testDS" type="DataSource">
      JdbcDriver org.apache.derby.jdbc.ClientDriver
      JdbcUrl jdbc:derby://localhost:1527/sample
      UserName app
      Password app
      </Resource>
      

      [No, I have not made any mistake there!!]

    • Save & close this file
    • Add following “datasource” in persistence.xml testDS
  10. Create JPA entity for Table “Contact”
    package com.mahendra.models;
    import java.io.Serializable;
    import javax.persistence.*;
    /**
    * The persistent class for the CONTACT database table.
    *
    */
    @Entity
    public class Contact implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int contactid;
    @Column(name="FIRST_NAME")
    private String firstName;
    @Column(name="LAST_NAME")
    private String lastName;
    @Column(name="PHONE_NO")
    private String phoneNo;
    public Contact() {
    }public int getContactid() {
    return this.contactid;
    }public void setContactid(int contactid) {
    this.contactid = contactid;
    }public String getFirstName() {
    return this.firstName;
    }
    
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    
    public String getLastName() {
    return this.lastName;
    }
    
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    
    public String getPhoneNo() {
    return this.phoneNo;
    }
    
    public void setPhoneNo(String phoneNo) {
    this.phoneNo = phoneNo;
    }
    
    }
  11. Your modified persistence.xml should look like this:
    <persistence version="2.0"
    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">
    <persistence-unit name="pu2">
    <jta-data-source>testDS</jta-data-source>
    <class>com.mahendra.models.Contact</class>
    </persistence-unit></persistence>
  12. Create new Java Class [It’s actually a session bean]
    package com.mahendra.services;
    import java.util.List;
    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    import javax.persistence.*;
    import com.mahendra.models.Contact;/**
    * Stateless Session bean to save and/or list all contacts<br/>
    * We are using Java EE 6 Web Profile, so we don't need EJB Project
    * to create Stateless Session Beans!<br>
    * This is a CMT (Again thanks to Apache TomEE, It CAN make JTA data-sources
    * unlike plain Apache Tomcat which is just a Servlet Container)
    * @author Mahendra Shinde
    *
    */@Stateless @LocalBean
    public class ContactService {
    @PersistenceContext(unitName="pu2")
    private EntityManager em;
    public ContactService(){
    System.out.println("Instance created for Contact Service");
    }
    /**
    * method to create new record using provided arguments<br/>
    * TransactionAttribute for this method is REQUIRED (default)
    * @param firstName
    * @param lastName
    * @param phone
    */
    
    public void save(String firstName,String lastName,String phone){
    Contact cn = new Contact();
    cn.setFirstName(firstName);
    cn.setLastName(lastName);
    cn.setPhoneNo(phone);
    em.persist(cn);
    System.out.println("Record saved");
    }
    
    /**
    * method to load all contacts from database
    * @return List of all contacts
    */
    public List<Contact> loadAll(){
    List<Contact> list = null;
    Query q =em.createQuery("select c from Contact c");
    list = q.getResultList();
    return list;
    }
    
    }
  13. Create Managed Bean (Backing bean) for JSF pages:
    package com.mahendra.beans;
    import java.io.Serializable;
    import java.util.List;
    import javax.ejb.EJB;
    import javax.faces.bean.*;
    import com.mahendra.models.Contact;
    import com.mahendra.services.ContactService;
    /**
    * Backing Bean for JSF pages. <br/>
    * This bean has reference of <code>ContactService</code> using @EJB Annotation
    * @author Mahendra Shinde
    *
    */
    @ManagedBean
    @SessionScopedpublic class ContactBean implements Serializable {
    @EJB private ContactService service;
    private String firstName,lastName,phone;
    private List<Contact> contactList;
    public ContactBean() {
    }public String getFirstName() {
    return firstName;
    }
    
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    
    public String getLastName() {
    return lastName;
    }
    
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    
    public String getPhone() {
    return phone;
    }
    
    public void setPhone(String phone) {
    this.phone = phone;
    }
    
    /**
    * Call save method on ContactService and update list
    * @return Outcome for JSF Navigation
    */
    
    public String addContact(){
    service.save(firstName,lastName,phone);
    contactList = service.loadAll();
    return "success";
    }
    
    public List<Contact> getContactList() {
    return contactList;
    }
    
    public void setContactList(List<Contact> contactList) {
    this.contactList = contactList;
    }
    
    }
  14. Add following navigation rule inside “faces-config.xml” file:
    <navigation-rule>
    <display-name>index.xhtml</display-name>
    <from-view-id>/index.xhtml</from-view-id>
    <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/list.xhtml</to-view-id>
    <redirect />
    </navigation-case>
    </navigation-rule>
  15. Create “index.xhtml” inside “WebContents” folder:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html" ><h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Home page</title>
    </h:head><h:body>
    <h:form>
    <h:outputText value="First name :" />
    <h:inputText value="#{contactBean.firstName}"/>
    <h:outputText value="Last name :" />
    <h:inputText value="#{contactBean.lastName}"/>
    <h:outputText value="Phone :" />
    <h:inputText value="#{contactBean.phone}"/>
    <h:commandButton value="submit" action="#{contactBean.addContact}"/>
    </h:form>
    </h:body>
    </html>
  16. create “list.xhtml” inside “WebContents” folder
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"><h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>List of Contacts</title>
    </h:head><h:body>
    <h2>My Contacts</h2>
    <h:dataTable value="#{contactBean.contactList}" var="c">
    <h:column>
    <f:facet name="header">Contact #</f:facet>
    #{c.contactid}
    </h:column>
    <h:column>
    <f:facet name="header">First Name</f:facet>
    #{c.firstName}
    </h:column>
    <h:column>
    <f:facet name="header">Last Name</f:facet>
    #{c.lastName}
    </h:column>
    <h:column>
    <f:facet name="header">Phone #</f:facet>
    #{c.phoneNo}
    </h:column>
    </h:dataTable>
    </h:body>
    </html>
    
  17. Now, run your application on Apache TomEE
    [Make sure Apache Derby is Still running]
    test url : http://localhost:8080/TestApp2/faces/index.xhtml

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