JSF html標(biāo)簽
命令類標(biāo)簽包括commandButton與commandLink
其主要作用在于提供一個(gè)命令按鈕或鏈接,以下舉例說明:
顯示一個(gè)命令按鈕,即輸出<input> HTML標(biāo)簽,其type屬性可以設(shè)定為button、submit或reset,默認(rèn)是submit,按下按鈕會(huì)觸發(fā) javax.faces.event.ActionEvent,使用例子如下:
<h:commandButton value="送出" action="#{user.verify}"/>
您可以設(shè)定image屬性,指定圖片的URL,設(shè)定了image屬性的話,<input>標(biāo)簽的type屬性會(huì)被設(shè)定為image,例如:
<h:commandButton value="#{msgs.commandText}"
image="images/logowiki.jpg"
action="#{user.verify}"/>
產(chǎn)生超鏈接,會(huì)輸出<a> HTML標(biāo)簽,而href屬性會(huì)有'#',而onclick屬性會(huì)含有一段JavaScript程序,這個(gè)JavaScript的目的是按下鏈接后自動(dòng)提交窗體,具體來說其作用就像按鈕,但外觀卻是超鏈接,包括在本體部份的內(nèi)容都會(huì)成為超鏈接的一部份,一個(gè)使用的例子如下:
<h:commandLink value="#{msgs.commandText}"
action="#{user.verify}"/>
產(chǎn)生的HTML輸出范例如下:
<a href="#" onclick="document.forms['_id3']['_id3:_idcl'].value='_id3:_id13'; document.forms['_id3'].submit(); return false;">Submit</a>
如果搭配<f:param>來使用,則所設(shè)定的參數(shù)會(huì)被當(dāng)作請(qǐng)求參數(shù)一并送出,例如:
<h:commandLink>
<h:outputText value="welcome"/>
<f:param name="locale" value="zh_TW"/>
</h:commandLink>
選擇類的標(biāo)簽可略分為單選標(biāo)簽與多選標(biāo)簽,依外型的不同可以分為單選鈕(Radio)、復(fù)選框(CheckBox)、列示方塊(ListBox)與選單(Menu)
以下分別先作簡單的說明:
- <h:selectBooleanCheckbox>
在視圖上呈現(xiàn)一個(gè)復(fù)選框,例如:
我同意 <h:selectBooleanCheckbox value="#{user.aggree}"/>
value所綁定的屬性必須接受與傳回boolean型態(tài)。
產(chǎn)生的html代碼:
我同意<INPUT TYPE="checkbox" />
- <h:selectOneRadio>、<h:selectOneListbox>、<h: selectOneMenu>
這三個(gè)標(biāo)簽的作用,是讓使用者從其所提供的選項(xiàng)中選擇一個(gè)項(xiàng)目,所不同的就是其外觀上的差別,例如:
<h:selectOneRadio value="#{user.education}">
<f:selectItem itemLabel="高中" itemValue="高中"/>
<f:selectItem itemLabel="大學(xué)" itemValue="大學(xué)"/>
<f:selectItem itemLabel="研究所以上" itemValue="研究所以上"/>
</h:selectOneRadio><p>
value所綁定的屬性可以接受字符串以外的型態(tài)或是自定義型態(tài),但記得如果是必須轉(zhuǎn)換的型態(tài)或自定義型態(tài),必須搭配 標(biāo)準(zhǔn)轉(zhuǎn)換器 或 自定義轉(zhuǎn)換器 來轉(zhuǎn)換為對(duì)象,<h:selectOneRadio>
產(chǎn)生的html代碼:
高中<INPUT TYPE="radio" NAME="1" value="高中"/>
大學(xué)<INPUT TYPE="radio" NAME="1" value="大學(xué)"/>
研究所以上<INPUT TYPE="radio" NAME="1" value="研究所以上">
您也可以設(shè)定layout屬性,可設(shè)定的屬性是lineDirection、pageDirection,預(yù)設(shè)是lineDirection,也就是由左到右來排列選項(xiàng),如果設(shè)定為pageDirection,則是由上至下排列選項(xiàng),例如設(shè)定為:
<h:selectOneRadio layout="pageDirection"
value="#{user.education}">
<f:selectItem itemLabel="高中" itemValue="高中"/>
<f:selectItem itemLabel="大學(xué)" itemValue="大學(xué)"/>
<f:selectItem itemLabel="研究所以上" itemValue="研究所以上"/>
</h:selectOneRadio><p>
<h:selectOneListbox>、<h: selectOneMenu>的設(shè)定方法類似于<h: selectOneRadio>
- <h:selectManyCheckbox>、<h:selectManyListbox>、<h: selectManyMenu>
這三個(gè)標(biāo)簽提供用戶復(fù)選項(xiàng)目的功能,一個(gè)<h:selectManyCheckbox>例子如下:
<h:selectManyCheckbox layout="pageDirection"
value="#{user.preferColors}">
<f:selectItem itemLabel="紅" itemValue="false"/>
<f:selectItem itemLabel="黃" itemValue="false"/>
<f:selectItem itemLabel="藍(lán)" itemValue="false"/>
</h:selectManyCheckbox><p>
value所綁定的屬性必須是數(shù)組或集合(Collection)對(duì)象,在這個(gè)例子中所使用的是boolean數(shù)組,例如:
package onlyfun.caterpillar;
public class UserBean {
private boolean[] preferColors;
public boolean[] getPreferColors() {
return preferColors;
}
public void setPreferColors(boolean[] preferColors) {
this.preferColors = preferColors;
}
......
}
如果是其它型態(tài)的對(duì)象,必要時(shí)必須搭配轉(zhuǎn)換器(Converter)進(jìn)行字符串與對(duì)象之間的轉(zhuǎn)換。
<h:selectManyListbox>的設(shè)定方法類似
選擇類標(biāo)簽可以搭配<f:selectItem>或<f:selectItems>標(biāo)簽來設(shè)定選項(xiàng),例如:
<f:selectItem itemLabel="高中"
itemValue="高中"
itemDescription="學(xué)歷"
itemDisabled="true"/>
itemLabel屬性設(shè)定顯示在網(wǎng)頁上的文字,itemValue設(shè)定發(fā)送至服務(wù)器端時(shí)的值,itemDescription 設(shè)定文字描述,它只作用于一些工具程序,對(duì)HTML沒有什么影響,itemDisabled設(shè)定是否選項(xiàng)是否作用,這些屬性也都可以使用JSF Expression Language來綁定至一個(gè)值。
<f:selectItem>也可以使用value來綁定一個(gè)傳回javax.faces.model.SelectItem的方法,例如:
<f:selectItem value="#{user.sex}"/>
則綁定的Bean上必須提供下面這個(gè)方法:
....
public SelectItem getSex() {
return new SelectItem("男");
}
....
如果要一次提供多個(gè)選項(xiàng),則可以使用<f:selectItems>,它的value綁定至一個(gè)提供傳回SelectItem?的數(shù)組、集合,或者是Map對(duì)象的方法,例如:
<h:selectOneRadio value="#{user.education}">
<f:selectItems value="#{user.educationItems}"/>
</h:selectOneRadio><p>
這個(gè)例子中<f:selectItems>的value綁定至user.educationItems,其內(nèi)容如下:
....
private SelectItem[] educationItems;
public SelectItem[] getEducationItems() {
if(educationItems == null) {
educationItems = new SelectItem[3];
educationItems[0] =
new SelectItem("高中", "高中");
educationItems[1] =
new SelectItem("大學(xué)", "大學(xué)");
educationItems[2] =
new SelectItem("研究所以上", "研究所以上");
}
return educationItems;
}
....
在這個(gè)例子中,SelectItem的第一個(gè)構(gòu)造參數(shù)用以設(shè)定value,而第二個(gè)參數(shù)用以設(shè)定label,SelectItem還提供有數(shù)個(gè)構(gòu)造函式,記得可以參考一下線上API文件。
您也可以提供一個(gè)傳回Map對(duì)象的方法,Map的key-value會(huì)分別作為選項(xiàng)的label-value,例如:
<h:selectManyCheckbox layout="pageDirection"
value="#{user.preferColors}">
<f:selectItems value="#{user.preferColorItems}"/>
</h:selectManyCheckbox><p>
您要提供下面的程序來搭配上面這個(gè)例子:
....
private Map preferColorItems;
public Map getPreferColorItems() {
if(preferColorItems == null) {
preferColorItems = new HashMap();
preferColorItems.put("紅", "Red");
preferColorItems.put("黃", "Yellow");
preferColorItems.put("藍(lán)", "Blue");
}
return preferColorItems;
}