comanndButton和commandLink:
commandLink必須要在一個(gè)from中。
comanndButton和commandLink要在一個(gè)from中才能提交表單內(nèi)容。
會(huì)發(fā)送回本頁面,并觸發(fā)JSF的生命周期,比如:重建組件樹、應(yīng)用請(qǐng)求值等,因此,允許設(shè)置actionListener和action屬性,這樣他們可以很輕松完成強(qiáng)大的功能。
<h:commandButton actionListener="#{actionListener.check}"; value="送出"; action="#{user.check}" />
actionListener 響應(yīng)的一個(gè)事件,當(dāng)然這個(gè)和js中的事件不一樣.actionListener="#{actionListener.check}"響應(yīng)的是服務(wù)器端的事件actionListener類的check方法.
action 用過STRUTS的都知道,提交后執(zhí)行的方法.當(dāng)然在SRTUTS中action="URL",而這里是一個(gè)user類的check方法.
不足在于:如果重建組件樹的成本比較高(比如:當(dāng)前頁面顯示一個(gè)數(shù)據(jù)表格),而這些組件對(duì)于即將跳轉(zhuǎn)到的頁面沒什么用時(shí),就應(yīng)該考慮使用outputLink了。
<h:commandLink action="#{user.testLink}"><f:verbatim>增加</f:verbatim></h:commandLink>
另外:如果需要傳遞參數(shù)<f:param.../>,使用commandLink
在action或actionListener中獲取<f:param.../>:
FacesContext ctx = FacesContext.getCurrentInstance();
int productId = Integer.parseInt((String)ctx.getExternalContext().getRequestParameterMap().get("productId"));
outputLink
比起前兩個(gè)來說,他相當(dāng)?shù)妮p量級(jí)了。他會(huì)直接產(chǎn)生一個(gè)<a href=""></a>鏈接,跳轉(zhuǎn)到相應(yīng)的頁面,因此沒有進(jìn)入JSF生命周期的額外開銷,跟我們直接寫一個(gè)html的鏈接沒什么區(qū)別。
如果需要傳遞參數(shù),嵌入<f:param name="a" value="b"/>就可以了,當(dāng)然這里的value可以用表達(dá)式來表示,比如value="#{param.productId}",用起來是相當(dāng)方便的。
<h:outputLink value="productEdit.faces">
<h:outputText value="編輯"/>
<f:param name="productId" value="#{item.productId}"/>
</h:outputLink>
其效果為<a href="..jsf?productId=..."></a>
h:commandButton、h:commandLink 和h:outputLink的差別在于:h:outputLink沒有進(jìn)入JSF的生命周期,而h:commandLink和h:commandButton都要進(jìn)入JSF的生命周期.