JSF 2.0 Simple Example

This section contain simple Hello World Example using JSF 2.0.

JSF 2.0 Simple Example

This section contain simple Hello World Example using JSF 2.0.

EXAMPLE

Create a new dynamic project as JSF2.0HelloWorld and select modify button :

After clicking modify following window will open ,select the following options :

Select JSF 2.0 in the above window with other options and click OK and click Next until following window will not appear to you :

 

Select JSF2.0 for Apache and download if not available using left corner Download library icon (Left corner second floppy type icon). After selecting library click Finish.

Create a package net.roseindia.jsf  and create a Java file HelloBean under it and paste the below content into it :

HelloBean.java

package net.roseindia.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import java.io.Serializable;

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

private static final long serialVersionUID = 1L;

private String name;

public String getName() {
return name;
}

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

Create following XHTML files under WebContent:

hello.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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">

<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example</h3>
<h:form>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Click" action="welcome"></h:commandButton>
</h:form>
</h:body>
</html>

welcome.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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">

<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body bgcolor="white">
<h2>JSF 2.0 Hello World Example</h2>
<h3><i>Welcome #{helloBean.name}</i></h3>
</h:body>
</html>

Modify the content of web.xml under WEB-INF as given below :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JSF2.0HelloWorld</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>

Now you are ready to run the application.

OUTPUT

The first screen after execution is given below :

When you enter some name for example Roseindia it will appear as follows :

Download Source Code

Advertisements

Share on Google+Share on Google+

JSF 2.0 Simple Example

Posted on: November 29, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JSF 2.0 Simple Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Harika
May 29, 2012
JSF2.0HelloWorld not working

Hi, I have done all the steps as mentioned. i have deployed the project war file in jboss 5.0 GA. I am getting this error: ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/JSF2.0HelloWorld].[Faces Servlet]] (http-127.0.0.1-8090-1) Servlet.service() for servlet Faces Servlet threw exception java.lang.StackOverflowError at org.apache.catalina.connector.Response.isAppCommitted(Response.java:331) at org.apache.catalina.connector.ResponseFacade.isCommitted(ResponseFacade.java:303) at org.apache.catalina.connector.ResponseFacade.addHeader(ResponseFacade.java:487) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158)........ ERROR [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-127.0.0.1-8090-1) JSF1054: (Phase ID: RENDER_RESPONSE 6, View ID: /hello.xhtml) Exception thrown during phase execution: javax.faces.event.PhaseEvent[[email protected]] ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/JSF2.0HelloWorld].[Faces Servlet]] (http-127.0.0.1-8090-1) Servlet.service() for servlet Faces Servlet threw exception java.lang.StackOverflowError at org.apache.catalina.connector.Response.isAppCommitted(Response.java:331) at org.apache.catalina.connector.ResponseFacade.isCommitted(ResponseFacade.java:303) at org.apache.catalina.connector.ResponseFacade.addHeader(ResponseFacade.java:487) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158) at javax.servlet.http.HttpServletResponseWrapper.addHeader(HttpServletResponseWrapper.java:158)... please help!!!

Ads

 

Ads