2011-01-05 58 views
4

我創建了一個Facelet組件來擴展h:commandLink(以添加一些功能和圓角)。擴展JSF commandLink組件

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core"> 
    <span class="btn-left btn-corners">&#160;</span> 
    <span type="submit" class="submit"> 
     <h:commandLink id="#{id}" value="#{label}" action="#{action}" /> 
    </span> 
    <span class="btn-right btn-corners">&#160;</span> </ui:composition> 

我的新組件可以使用

<my:commandLink id="continue" label="continue" action="#{applyBacking.submit}"/> 

訪問和Java代碼

public String submit(){ 
    ... 
} 

但是它給了我一個錯誤 「ApplyBacking沒有物業提交」。 我明白這個錯誤的原因,因爲在呈現my:commandLink時,它會嘗試評估#{applyBacking.submit}屬性。相反,我希望關於調用的方法(applyBacking.submit)的信息被傳遞給模板並在渲染h:commandLink時進行評估。

有什麼建議嗎?

回答

5

改爲創建composite componenttutorial here),它使您可以將bean操作定義爲attribtues。

這裏有一個開球例如:

/resources/components/commandLink.xhtml

<ui:component 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:cc="http://java.sun.com/jsf/composite"> 
    <cc:interface> 
     <cc:attribute name="id" required="true" /> 
     <cc:attribute name="label" required="true" /> 
     <cc:attribute name="action" method-signature="java.lang.String action()" required="true" /> 
    </cc:interface> 
    <cc:implementation> 
     <span class="btn-left btn-corners">&#160;</span> 
     <span type="submit" class="submit"> 
      <h:commandLink id="#{cc.attrs.id}" value="#{cc.attrs.label}" action="#{cc.attrs.action}" /> 
     </span> 
     <span class="btn-right btn-corners">&#160;</span> 
    </cc:implementation> 
</ui:component> 

/somepage.xhtml

<!DOCTYPE html> 
<html lang="en" 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:my="http://java.sun.com/jsf/composite/components"> 
    <h:head> 
     <title>SO question 4608030</title> 
    </h:head> 
    <h:body> 
     <h:form> 
      <my:commandLink id="continue" label="continue" action="#{applyBacking.submit}"/> 
     </h:form> 
    </h:body> 
</html> 

順便說一句,我個人比較喜歡使用JS/jQuery的的圓角部分,例如jQuery corner plugin。只要給你的命令鏈接一個特定的styleClass,讓JS做魔術。