第一部份我們先來看最簡單的使用(僅供教學參考,不適合在實際案例中使用):
JSF xhtml
結果頁 java bean 內容
/**
* 表單輸入程式示範
*/
package jason.blogger.jsf;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
/**
* @author jason
* 2013/5/19
*/
@ManagedBean(name = "appForm")
public class ApplyForm implements Serializable
{
private static final long serialVersionUID = 1L;
private String userId; // 使用者代號
private String userName; // 使用者名稱
private String password; // 密碼
private String gender; // 性別
private String city; // 城市
/**
* @return the userId
*/
public String getUserId()
{
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(String userId)
{
this.userId = userId;
}
/**
* @return the userName
*/
public String getUserName()
{
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName)
{
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return the gender
*/
public String getGender()
{
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender)
{
this.gender = gender;
}
/**
* @return the city
*/
public String getCity()
{
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city)
{
this.city = city;
}
}


在這個 h:inputText 案例中,呈現了一個完整而簡單的 JSF 概念。有以下幾點值得說明:
1. outputText 與 outputLabel 的不同:
outputLabel 轉換成 html 時會變成 <label> 標籤,而 outputText 會直接輸出文字,這在意義上是不同的。
2.ManagedBean的使用:
JSF 2.0 在 Java Class 中上方加上 @ManagedBean 就可以讓這個 Bean 變成 ManagedBean,並且指定 ManagedBean 的名字。(預設未指定 ManagedBean 名稱,則以 class 名稱第一個字母小寫為名,如上例若未指定 name 則 bean name 為 appForm)
未特別指定 Bean 的 Scope ,它預設為 request scoped。
3.commandButton 的基本頁面導向:
JSF 2.0 CommandButton 的 action 屬性可以直接用字串指定要前往的頁面。 指定 /xhtml/resultPage 時,按下 commandButton 會被導到 xhtml 目錄下的 resultPage.jsf 頁面。
