2011-03-07 23 views
6

我想根據點擊按鈕來包含特定的頁面。使用f:attribute作爲commandButton而不是f:param作爲h:commandLink

到目前爲止使用h:commandButton,我無法使用f:param,所以它看起來像我應該使用f:attribute標記。

f:param情況下,我想這樣的代碼:

<h:commandLink action="connectedFilein"> 
    <f:param name="fileId" value="#{fileRecord.fileId}"/> 
<h:commandLink> 

<c:if test="#{requestParameters.fileId!=null}"> 
    <ui:include src="fileOut.xhtml" id="searchOutResults"/> 
</c:if> 

什麼是f:attribuite情況?

感謝

回答

14

我假設你正在使用JSF 1.x中,否則這個問題沒有意義。在舊版JSF 1.x中確實不支持<f:param>,<h:commandButton>,但自從JSF 2.0以後,它就受到支持。

<f:attribute>可以與actionListener結合使用。

<h:commandButton action="connectedFilein" actionListener="#{bean.listener}"> 
    <f:attribute name="fileId" value="#{fileRecord.fileId}" /> 
</h:commandButton> 

public void listener(ActionEvent event) { 
    this.fileId = (Long) event.getComponent().getAttributes().get("fileId"); 
} 

(假設它是Long類型,這是經典的用於ID的)


更好然而,使用引入<f:setPropertyActionListener>的JSF 1.2。

<h:commandButton action="connectedFilein"> 
    <f:setPropertyActionListener target="#{bean.fileId}" value="#{fileRecord.fileId}" /> 
</h:commandButton> 

或者當你已經在運行一個Servlet 3.0/2.2 EL能夠容器(Tomcat的7,Glassfish的3等),您的web.xml聲明符合的Servlet 3.0,那麼你可以只把它作爲方法參數。

<h:commandButton action="#{bean.show(fileRecord.fileId)}" /> 

public String show(Long fileId) { 
    this.fileId = fileId; 
    return "connectedFilein"; 
} 

無關的具體問題,我強烈推薦,而不是使用JSTL那些儘可能 JSF/Facelets標記。

<ui:fragment rendered="#{bean.fileId != null}"> 
    <ui:include src="fileOut.xhtml" id="searchOutResults"/> 
</ui:fragment> 

(A <h:panelGroup>也可以和最好的方法使用JSP,而不是Facelets的時候)

+0

任何情況下,我應該使用bean.property聲明這種情況下?請求參數不在這裏播放?我想讓它更簡單 – sergionni 2011-03-07 15:33:13

+0

JSF 2.0在''中支持''。所以如果可以的話,升級它。否則你現在最好的選擇是''。 – BalusC 2011-03-07 15:39:32

+0

好吧,我看到,不幸的是我們在我們的項目中使用了JSF 1.2。謝謝 – sergionni 2011-03-07 15:41:01

相關問題