10:11 AM
0




Hello friends, here in this post we are providing basic learning concepts in Java Server Faces 1.2 .
After learning this example you can easily develop a JSF application with good understanding.

Steps to develop JSF application:

1.Install tomcat
2.Download JSF 1.2 jar files
3.create directory structure
4.deploying application

1.Install Tomcat:

This is very easy process . Just we have to download latest version of tomcat from given link.
http://tomcat.apache.org/download-60.cgi .After downloading the tomcat we have to check whether it is working or not. by placing http://localhost:8080/ . It will open tomcat home page. If you get tomcat home page then our installation is completed successfully.

2.Download JSF 1.2 jar files:

In order to do this application we have to download some jar files related to JSF . Those are 
1.jsf-api-1.2.jar
2.jsf-impl.jar
3.jstl.jar
4.jstl-api-1.2-sources.jar

3.Create Directory Structure:

1.Create a directory "jsf" under "apache-tomcat\webapps"
2.Create a folder with name WEB-INF under \webapps\WEB-INF
3.Create 2 folders with names lib and classes under WEB-INF 
4.Create a file web.xml under WEB-INF folder

web.xml:

<?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">

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

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>


</web-app>

5.Create a file faces-config.xml under WEB-INF folder 

faces-config.xml:

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

<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_1_2.xsd"
version="1.2">

<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>result</from-outcome>
<to-view-id>/display.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<managed-bean>
<managed-bean-name>Bean</managed-bean-name>
<managed-bean-class>com.buddhi.Bean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean> 

</faces-config>

6.Copy all jar files which we have downloaded earlier into lib folder

7.Create a html files and placed under WEB-INF folder

index.html:

<HTML>
<HEAD>
<meta http-equiv="Refresh" content= "0; URL=index.faces"/>
</HEAD>

</HTML>

8.Create jsp files and place under same WEB-INF folder only

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<body>
<f:view><h:form>
<h:messages style="color: red;"></h:messages>
<table>
<h:outputText value="Enter following information" />
<tr><td>
<h:outputText value="Name" />
</td><td>
<h:inputText id="name" value="#{Bean.name}" required="true">
<f:validateLength minimum="2" maximum="25"></f:validateLength>
</h:inputText></td></tr>
<tr><td>
<h:outputText value="Department " />
</td><td>
<h:inputText id="dept" value="#{Bean.dept}" required="true">
<f:validateLength minimum="2" maximum="25"></f:validateLength>
</h:inputText>
</td></tr><tr><td>
<h:outputText value="Roll No." />
</td><td>
<h:inputText id="rollno" value="#{Bean.rollno}" required="true">
<f:validateLength minimum="2" maximum="25"></f:validateLength>
</h:inputText></td></tr><tr><td>
<h:commandButton id="button" action="result" value="Submit" />
</td></tr></table></h:form>
</f:view>
</body>

</html>


display.jsp


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>greeting page</title>
</head>
<body>
<f:view>
<Table>
<tr><td>
<h:outputText value="Student Name:-#{Bean.name}" />
</td></tr><tr><td>
<h:outputText value="Department Name:-#{Bean.dept}" />
</td></tr><tr><td>
<h:outputText value="Roll no:-#{Bean.rollno}" />
</td></tr><tr>
<td><a href="/FirstJSF/index.faces">Back</a></td>
</tr>
</Table>
</f:view>
</body>
</html>

9.After this we have to write bean class and place that class file under classes folder

Bean.java

package com.buddhi;
public class Bean 
{

private String name;
private String dept;
private int rollno;

public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
}

4.Deploying Application:

Now we reached final stage of application i.e running on browser

http://localhost:8080/ it will redirect our application to index.faces.

if you provide name,department and roll number in text boxes appeared on screen, then they will display on output page.

0 comments:

Post a Comment