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>;

}
Advertisements

One comment on “JSF sample app: part 3

  1. Pingback: JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2 | mahendra's Unlimited Blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s