Anda di halaman 1dari 8

JSF Life cycle methods:

SubmitWebpage------>Create or restor the view---->Apply values form users---->En


sure values are valid------->Update model with valid values------->
Fetch a new view if necessarry------->Renderview---->view Webpage.
How can you write a input value..........
Syntax:<h: inputText value ="#{modelBean.username}"/>
JSF follows model view control design patern.........
web.xml file
<web-app version="2.5"
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/javee/web-app_2_5.xsd>
<display-name>JSFReg</disaplay-name>
<description>Simple Registration Application</description>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>faces/register.xhtml</welcome-file>
</welcome-file-list>
</web-app>
-----------------------------------------------------------------------------------------------------------------------------------------------------------web.xml:register.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title> A Simple JavaServer Faces Registration Application</title>
</h:head>
<h:body>
<h:form>
<h2>JSF Registration App</h2>
<h4>Registration Form</h4>
<table>
<tr>
<td>First Name:</td>
<td>
<h:inputText label="First Name"
id="fname"value="#{userBean.firstName}"
required="true"/>

<h:message for ="fname"/>


</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<h:inputText label="Last Name"
id="lname" value="#{userBean.lastName}"
required="true"/>
<h:message for="lname"/>
</td>
</tr>
<tr>
<td>Sex:</td>
<td>
<h:selectOneRadio label="Sex"
id="sex' value="#{userBean.sex}"
required="true">
<f:selectItem itemLable="Male"itemValue="male"/>
<f:selectItem itemLable="Female"itemvalue="female"/>
</h:selectOneRadio>
<h:message for ="sex"/>
</td>
</tr>
<tr>
<td>Date Of Birth:</td>
</td>
</tr>
<tr>
<td>
<h:inputText label="Date Of Birth"
id="dob"value="#{userBean.dob}"required="true">
<f:convertDateTime pattern="mm-dd-yy"/>
</h:inputText>(mm-dd-yy)
<h:message for ="dob"/>
</td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<h:inputText label = "Email Address"
id = "email" value="#{userBean.email}"
required="true"
validator="#{userBean.validateEmail}"/>
<h:message for = "email"/>
</td>
</tr>
<tr>
<td> Service Level:</td>
<td>
<h:selectOneMenu label="serviceLevel"
value="#{userBean.serviceLevel}">
<f:selectItem itemLabel="Medium itemvalue="medium"/>
<f:selectItem itemLabel="Basic"itemValue="basic"/>
</h:selectOneMeny>
</td>
</tr>
</table>
<p><h:message/></p>
<p><h:commandButton value="Register"action="confirm"/>

</P>
</h:form>
<h:body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------UserBean.java
import
import
import
import
import
import
import

java.util.Date;
javax.faces.application.FacesMessage;
javax.faces.component.UIComponent;
javax.faces.context.Facescontext;
javax.faces.bean.ManagedBean;
javax.faces.bean.SessionScoped;
javax.faces.validator.validatorException;

@ManagedBean
@SessionScoped
public class UserBean{
protected String firstName;
protected String lastName;
protected Date dob;
Protected String sex;
Protected String email;
Protected String serviceLevel="medium;
Public String getFirstName() {
return firstName;
}
Public void setFirstName(String firstName) {
this.firstName;
}
Public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getSex() {
this.sex=sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email =email;
}
public String getServiceLevel() {
return serveceLevel;
}
public void setServiceLevel(String serviceLevel) {
this.serviceLevel = serviceLevel;
}

public void validateEmail(FacesContext context,UIcomponent toValidate,ob


ject value)throws ValidatorException {
String emailStr = (String) value;
if (-1 == emailStr.indexOf("@")) {
FacesMessage message = new FacesMessage("Invalid email address");
throw new validatorException(message);
}
}
public String addconfirmedUser() {
boolean added = true;//actual application may fail to add user
FacesMessage doneMessage = null;
if(added) {
doneMessage = new FacesMessage("Sucessfully added new user");
outcome = "done";
} else {
doneMessage = new FacesMessage("Failed to add new user");
outcome = "register"l;
}
Facescontext.getCurrnetInstance().addMessage(null,doneMessage);
return outcome;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------how to create a viewpage-----<!DocTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml-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">
<body>
<ui:compostition template="/lnf-template.xhtml">
<ui:define name="title">
JavaServer Faces Virtual Trainer Application
Regisrtion </ui:define>
<ui:define name="body">
<table width="70%">
<tr>
<td width="40%">First Name:</td>
<td width="60%">
<input type="text" jsfc="h:inputText"required="true"
id="fname"
value="#{UserBean.firstname}"/>
<h:message for = "fname"/>
</td>
</tr>
<tr>
<td width="40%">Last Name:</td>
<td width="60%">
<input type="text"jsfc="h:inputText"
Value="#{UserBean.lastname}" required="true"
id="lname"/>

<span jsfc="h;message"for="fname">message</span>
</td>
</tr>
<tr>
<td width="40%">Gender:</td>
<td width="60%">
<h:selectoneRadio type="radio"
value="#{UserBean.gender}"required="true"id="gender">
<input type ="radio"jsfc="f:selectItem"
itemLabel="Male"itemValue="male"/>
<input type="radio"jsfc="f:selectItem"
itemLabel="Male" itemValue="male"/>
<input type="radio" jsfc="f:selectItem"
itemLabel="Female" itemValue="female"/>
</h:selecctOneRadio>
<h:message for="gender"?>
</td>
</tr>
<tr>
<td width="40%>Date Of Birth:</td>
<td width="60%">
<h:inputText value ="#{UserBean.dob}"id="dob" required="true">
<f:convertDateTime pattern="mm-dd-yy"/>
</h:inputTect>(mm-dd-yy)
<h:message for="dob"/> </td>
</tr>
<tr>
<td width="40%">Email Address:</td>
<td width="60%">
<h:inputText value="#{UserBean.email}"required="true"
validator="#{UserBean.validateEmail}"id="email"?>
<h:message for ="email"/>
</td>
</tr>
<!---Remainder of rows deleted for brevity---->
</table>
</ui:define>
</ui:composition>
</body>
</html>
________________________________________________________________________________
________________________________________________________________________________
____________
jsf sample example tutorials:
http://www.developersbook.com/jsf/jsf-tutorial/jsf-tutorial-2.php
----------------------------------------------------------------------------------------------------------------------------------------jsf Basic Example:
personBean.Java
-----------------------------------------------------------------package com.developersBook.jsf.bean;
public class PersonBean {

private String name;


public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
-------------------------------------------------------------------------------Index.jsp
-----------------------------------------------------------------------------------------------------------------<html>
<%@taglib uri ="http://java.sun.com/jsf/core"prefix="f"%>
<%@taglib uri ="http://java.sun.com/jsf/html"prefix="h"%>
<f:view>
<head><title>JSF Hello World</title></head>
<body>
<h:form id="helloForm">
<h:outputLabel for= "promptName"value="Please enter your name"/>
<h:panelGrid columns="2">
<h:outputLabel for="name' value="Name:"/>
<h:inputTextid ="name"value="#{person.name}"/>
<h:commandButton value="SayBye" action="SayBye"/>
</h:panelGrid>
</h:form>
</body>
</f:view>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------Hello.jsp
________________________________________________________________________________
____________________________________
<%@ taglib uri="http://java.sun.com/jsf/html"prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core"prefix="f"%>
<html>
<f:view>
<head><title>Hello World!</title></head>
<body>
<h:form>
<h3>Hello,<h:outputText value="#{person.name}"
<h:commandLink id="cancel"action="index"immediate="true">
<h:outputText value="Cancel"/>
</h:commandLink>
</h:form>
</body
</f:view>
</html>
----------------------------------------------------------------------------------------------------------------

Bye.jsp
-------------------------------------------------------------------------------------------------------------------------------<%@ taglib uri="http://java.sun.com/jsf/html"prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core"prefix="f"%>
<html>
<f:view>
<head><title>GoodBye World!</title></head>
<body>
<h:form>
<h3>GoodBye,<h:outputText value="#{person.name}"/></h3>
<h:commandLink id="Cancel"action="index"immediate="true">
<h:outputText value="Cancel"/>
</h:commandLink>
<h:form>
</body>
</f:view>
</html>
-------------------------------------------------------------------------------------------------------------------------web.xml
------------------------------------------------------------------------------------------------------------------------------<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlnsL:xsi="http://www.w3.org/2001/XML/ns/javaee"
xsi:schemaLocation="http://java.sun.com.xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-c
lass>
<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.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----------------------------------------------------------------------------------------------------------------------------------JSF sample programs site:

http://www.roseindia.net/jsf/richfaces/developing-jsp-files.shtml
--------------------------------------------------------------------------------

---------------------------------------------------------------------------------Seem Example site:


http://thelabdude.blogspot.com/2010/05/building-intelligent-web-app-using-seam.h
tml
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------prime faces:
http://www.primefaces.org/
http://code.google.com/p/primefaces/source/browse/examples/trunk/showcase/src/ma
in/webapp/mobile/weather.xhtml
------------------------------------------------------------------------------------------------------------------------------------------------------------------------how to create a jsf sample program site
http://exadel.com/web/portal/jsftutorial-kickstart
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Anda mungkin juga menyukai