t; <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Servlet Mapping to URL pattern --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> </web-app>

Add “javax.faces.webapp.FacesServlet” servlet and map it to different URL patterns as below –

  • *.xhtml
  • *.jsf
  • *.faces

In JSF 2.0 development, it’s recommended to set the “javax.faces.PROJECT_STAGE” to “Development“, it will provide many useful debugging information to let you track the bugs easily. For deployment, just change it to “Production“.

JSF 2.0 Managed Bean

Managed bean is nothing but a regular java bean which has received as fancy name in JSF terminology. Whenever a bean is registered with the JavaServer Faces it becomes a managed bean. Managed beans are used as a model for UI components. Prior to JSF 2.0, managed bean was registered in faces-config.xml but now in JSF 2.0, there is no need to declare managed bean in the config xml file. Instead in JSF 2.0 managed beans are registered using @managedbean annotation.

Create a managed bean in package com.kodehelp.jsf as below. You can prefer any package name.

package com.kodehelp.jsf;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class HelloWorldBean implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private String firstName;

	private String lastName;

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

}

JSF 2.0 UI Pages

The standard for creating the UI pages is in .xhtml format and this is recommended in JSF.

I will be writing on UI Tags soon. Please keep watching http://kodehelp.com

To use JSF tags in UI page you need to include below namespaces in the html tag –

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

To use application.properties file you need to add below JSF tag –

<f:loadBundle basename="resources.application" var="msg"/>

And you a can use as the application.properties as shown below –

# -- welcome --
welcomeTitle=JSF Hello World Application

welcomeHeading=Welcome!

welcomeMessage=This is a JSF Hello World application. \
	Please Enter your First Name and Last Name below.

helloHeading=Hello!

helloMessage=This is a JSF Hello World application. \
	Enjoy your JSF Learning and there is much more to learn in JSF 2.0. \
	Please stay with Vigilance series of JSF 2.0 Tutorial at

 

<head>
    <title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>

File: index.xhtml – 

  • Renders 2 outputText which shows the property value from application.properties file.
  • Renders 2 outputLabel for First Name and Last Name.
  • Renders 2 textbox for firstName and lastName which are attached with respective managed bean property.
  • Renders a button which displays hello.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<f:loadBundle basename="resources.application" var="msg"/>

<head>
    <title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>

<body>
<h3><h:outputText value="#{msg.welcomeHeading}" /></h3>
<p><h:outputText value="#{msg.welcomeMessage}" /></p>
<h:form>
  <h:panelGrid columns="2">
   <h:outputLabel for="firstName">First Name *</h:outputLabel>
   <h:inputText id="firstName" value="#{helloWorldBean.firstName}"></h:inputText>

   <h:outputLabel for="lastName">Last Name *</h:outputLabel>
   <h:inputText id="lastName" value="#{helloWorldBean.lastName}"></h:inputText>

    <h:commandButton value="Submit" action="hello"></h:commandButton>

  </h:panelGrid>
 </h:form>
</body>
</html>

File: hello.xhtml – This page renders the value from the managed bean for First name and Last Name entered on the index.xhtml.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html">
<f:loadBundle basename="resources.application" var="msg"/>

<head>
    <title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<h:body>
 <h3><h:outputText value="#{msg.helloHeading}" />&nbsp;#{helloWorldBean.firstName}&nbsp;#{helloWorldBean.lastName}</h3>
<p><h:outputText value="#{msg.helloMessage}" /><a href="http://vigilance.o.in">Vigilance.co.in</a></p>
</h:body>
</html>

The #{ } indicate as a JSF expression language, in this case, #{helloWorldBean.firstName} and #{helloWorldBean.lastName}, when the page is submitted, JSF will find the “helloWorldBean” and set the submitted textbox value via the setFirstName() method. When hello.xhtml page is displayed, JSF will find the “helloWorldBean” and displays the firstName and lastName property value via the getFirstName() and getLastName() methods respectively.

Running the JSF 2.0 Hello World Application

To run this application from eclipse, right click on the project à Run As à Run On Server

Select the tomcat 7.0 server and run the application.

Go to url http://localhost:8080/JavaServerFaces/ for view the page.

index.xhtml

Video Tutorial: Getting Started with Struts

Struts is the one of the most famous, classic and proven Model-View-Contraller (MVC) framework. Struts 1.x is a complete web framework, provides complete web form components, validator, internalization, error handling, tiles layout, low learning curve and easy to implement it. The framework provides three key components:

  • A “request” handler provided by the application developer that is mapped to a standard URI.
  • A “response” handler that transfers control to another resource which completes the response.
  • A tag library that helps developers create interactive form-based applications with server pages.

Below video tutorial will show you how to kick start with Struts basics –

-->

How to make SFTP or SSH connection using public key authentication in java?

How to make SFTP or SSH connection using public key authentication in java?

By May 11, 2015 at 7:32 pm · How To

Using SSH public key authentication to make the connection to remote system is more secure and robust method than login with account password. SSH public key authentication is an authentication method that relies on asymmetric cryptographic algorithms that generate a pair of  separate keys, one private and the other public. Private Key is the secret key which is…
Read more »



How to write Bubble Sort in Java ?

How to write Bubble Sort in Java ?

By September 16, 2014 at 12:58 am · JAVA API, sorting

We all have learnt sorting techniques in our college academic curriculum. Out of those sorting techniques the simplest technique we learnt is Bubble Sort. The Bubble sort takes an array of numbers and sort them within the array indexes in either ascending or descending order. Bubble sort is simplest of all sorting techniques but it…
Read more »


How To Send a Plain Text EMail in Java using JavaMail API ?

How To Send a Plain Text EMail in Java using JavaMail API ?

By May 5, 2014 at 12:28 am · javax.mail

Java Mail API is used to create your own email client. This class provides the code required to model your email client similar to Outlook, Hotmail and many others. In JavaMail you’ll find APIs and provider implementations allowing you to develop fully functional email client applications. Below java program shows you how to send a plain text…
Read more »


How to use Preferences API to store application/Program configurations ?

How to use Preferences API to store application/Program configurations ?

By October 16, 2013 at 3:32 am · java.util

Introduction Preferences (Java.util.pref) API is included in JDK can be used to remember and retrieve application preferences. Using this API you can manage preference data such as storing, retrieving, removing and cleaning preference data. This API is not intended to save application data. Also it reduces the burden of application developer to write the code…
Read more »


Nodejs HelloWorld Example

Nodejs HelloWorld Example

By January 2, 2013 at 5:08 am · Nodejs

This tutorial is the quick guide to show you how to write the classic “Hello World!!!” example in Nodejs. This example is using the 0.8.x version of Nodejs installed on Windows 7. Install Nodejs Refer this article for installing Node.js on Windows. This installation very easy and it starts with downloading the executable on windows…
Read more »


Installing Node.js on Windows

Installing Node.js on Windows

By January 2, 2013 at 4:25 am · Nodejs

In this tutorial we will show you how to install the Node.js on Windows and Linux. This example shows you installation of Node.js version 0.8.16. 1. Node.js Installation on Windows Below steps shows you how to install Node.js on Windows – Download Node.js Download Node.js from official Node.js website. Choose appropriate version and click to…
Read more »


JavaServer Faces (JSF) 2.0 Tutorial

JavaServer Faces (JSF) 2.0 Tutorial

By September 16, 2012 at 3:28 pm · JSF 2.0

Introduction to JavaServer™ Faces (JSF) Java Server Faces (JSF) is the MVC web framework based on Java Programming Language. JSF mainly focus on simplifying the building of web user interface by providing ready to use UI tags. It simplifies the assembling of reusable UI components in a page. Javaserver Faces is based on a component-driven…
Read more »


JavaServer Faces (JSF) 2.0: Hello World Example

JavaServer Faces (JSF) 2.0: Hello World Example

By September 16, 2012 at 3:18 pm · JSF 2.0

Download Source code for tutorial Download here – JavaServerFaces 2.0 – Hello World Example – Source code Setup Development Environment To start with JavaServer Faces Hello World Example, you need to setup development environment. Below list shows you, how to setup tutorial’s development environment, dependencies and basic configurations. Eclipse 3.7 (Indigo Release) If you don’t have…
Read more »


Video Tutorial: Getting Started with Struts

Video Tutorial: Getting Started with Struts

By September 2, 2012 at 9:48 pm · Struts

Struts is the one of the most famous, classic and proven Model-View-Contraller (MVC) framework. Struts 1.x is a complete web framework, provides complete web form components, validator, internalization, error handling, tiles layout, low learning curve and easy to implement it. The framework provides three key components: A “request” handler provided by the application developer that is mapped…
Read more »