透過文字欄位輸入內容後在顯示於UI上或傳遞至物件中
h:form給予Id名稱,目的是按下確認鈕後,刷新h:form標籤內的內容
<h:form id="form">
按鈕的Primefaces標籤如下,並設定update屬性對象Id為form
即更新<h:form id="form"> 至 </h:form>涵蓋之內容
<p:commandButton value="確定" update="form"/>
【完整程式碼-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" xmlns:p="http://primefaces.org/ui"> <h:head> <title>基本文字輸入</title> </h:head> <h:body> <h:form id="form"> <h:outputLabel value="姓名: " /> <p:inputText value="#{peopleBean.name}"/> <br/> <h:outputLabel value="年齡: " /> <p:inputText value="#{peopleBean.age}"/> <br/> <h:outputLabel value="性別: " /> <p:inputText value="#{peopleBean.gender}"/> <br/> <h:outputLabel value="國籍: " /> <p:inputText value="#{peopleBean.nationality}"/> <br/><br/> <p:commandButton value="確定" update="form"/> <br/><br/> <h:outputText value="姓名:#{peopleBean.name}" /> <br/> <h:outputText value="年齡:#{peopleBean.age}" /> <br/> <h:outputText value="性別:#{peopleBean.gender}" /> <br/> <h:outputText value="國籍:#{peopleBean.nationality}" /> </h:form> </h:body> </html>
【完整程式碼-java】
import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean (name="peopleBean") @ViewScoped public class PeopleBean implements Serializable{ private static final long serialVersionUID = -7485713690762093029L; private String name; private int age; private String gender; private String nationality; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setGender(String gender){ this.gender = gender; } public String getGender(){ return gender; } public void setNationality(String nationality){ this.nationality = nationality; } public String getNationality(){ return nationality; } }【瀏覽器執行結果】
預設頁面
按下確定後刷新內容
至於更進階的欄位輸入,如下拉選單以及介面的排版方式等...後續文章會繼續介紹唷!!
沒有留言:
張貼留言