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