Hai Readers as i said in the previous JSF introduction post today we are going to learn the basic example in JSF,
You need two types of jar files
1.jstl.jar
2.standard.jar
and two tld files
1.jsf_core.tld
2.html_basic.tld
to run a basic JSF program
First create the basic JSF program,create a jsp file simple.jsp later in web.xml it is converted and run as index.jsf now enter the code below
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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:message for="value"/>
<h:inputText id="value" value="#{simple.field}">
<f:convertDateTime type="date" pattern="dd/mm/yyyy"/>
</h:inputText>
<h:inputText id="date" value="#{simple.date}"/>
<h:commandButton action="#{simple.submit}" value="submit"/>
</h:form>
</f:view>
</body>
</html>
In this file there is a tag to show the message about errors h:message and two inputtext box, one getting current date from bean and another inputtext box where we are going to enter the data and check whether it is in correct format.
Lets create the bean for the program Simplebean.java
package s2sgateway;
import java.util.Date;
public class Simplebean {
private String field;
public Date date;
public Date getDate()
{
date=new Date();
return date;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String submit()
{
System.out.println(this.field);
System.out.println(this.date);
return "success";
}
}
Now lets create the faces-config.xml and enter the values such as bean name,class and scope values
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config >
<managed-bean>
<managed-bean-name>simple</managed-bean-name>
<managed-bean-class>s2sgateway.Simplebean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
Now in web.xml file the the servelt-name->Faces Servlet&
url-pattern will be *.jsf so that you can run the file with .jsf extension like index.jsf otherwise you have to run as index.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Now you can run this program when you enter any wrong date or no it shows conversion error if you give the date as 31/8/2010 then it doesn’t shows any error,Any doubt ask us happy coding
simplejsf Happy coding.