2013-04-11 28 views
1

我想要一個可點擊的圖片,點擊時發送兩個參數給我的動作。我用<h:graphicImage>嘗試了以下內容。如何創建一個可點擊的圖片,用參數調用一個動作

<h:graphicImage url="/pic/down.png" alt="download"> 
    <h:outputLink action="#{file.download}"> 
     <f:param name="id" value="#{file.id}"></f:param> 
     <f:param name="name" value="#{file.name}"></f:param> 
    </h:outputLink> 
</h:graphicImage> 

然而,它沒有奏效。我如何正確實現這一目標?

回答

4

如果您知道basic HTML會更容易。在純HTML一個可點擊的圖片是這樣的:

<a href="..."><img src="..." /></a> 

然而,你的JSF代碼生成是這樣的(右擊,查看源在瀏覽器中看到它):

<img src="..."><a href=" ..." /></img> 

這是invalid HTML。你需要交換它們。

此外,我不確定這是否是粗心撥弄/在黑暗中拍攝的結果,但在<h:outputLink>中不支持action屬性。你可能喜歡用<h:commandLink>

因此,所有的一切,這應該這樣做:

<h:commandLink action="#{file.download}"> 
    <h:graphicImage value="/pic/down.png" alt="download" /> 
    <f:param name="id" value="#{file.id}" /> 
    <f:param name="name" value="#{file.name}" /> 
</h:commandLink> 

(確保這是擺在<h:form>

我不確定但是它是如何有意義傳遞的性質#{file}回到顯然與您調用操作的地方相同的#{file}實例。你也可以省略那些<f:param>的東西,並直接在action方法中訪問屬性。

<h:commandLink action="#{file.download}"> 
    <h:graphicImage value="/pic/down.png" alt="download" /> 
</h:commandLink> 
相關問題