2013-08-23 26 views
0

創建了一個TagHandler並添加了一個facelet。 facelet內容仍未評估。該html代碼包含ui:片段文本。面對包含jsf組件的組件內容未評估

@Override 
    public void encodeBegin(FacesContext context) throws IOException{ 
    ResponseWriter writer = context.getResponseWriter(); 
       content = benefits.getContent(type); 
    writer.write(content); 
} 

    <content type="short"> 
    <data><![CDATA[   
    <ui:fragment rendered="#{true}"> 
     <a id="" href="a.xhtml> 
    </ui:fragment> 
    <ui:fragment rendered="{false}"> 
     <a id="" href="b.xhtml"> 
    </ui:fragment> 
    <img src="a.png" alt="" /> 
    </a> ]]></data> 

    public class CardHolderBenefitsTagHandler extends TagHandler { 

private final TagAttribute src; 

public CardHolderBenefitsTagHandler(TagConfig config) { 
    super(config); 
    TagAttribute attr = null; 
    attr = this.getAttribute("src");    
    this.src = attr; 

} 

public void apply(FaceletContext ctx, UIComponent parent) 
     throws IOException { 
    String path = this.src.getValue(ctx); 

    VariableMapper orig = ctx.getVariableMapper(); 
    ctx.setVariableMapper(new VariableMapperWrapper(orig)); 
    try { 
     this.nextHandler.apply(ctx, null); 
     ctx.includeFacelet(parent, path); 
    } catch (IOException e) {   
     throw new TagAttributeException(this.tag, this.src, 
       "Invalid path : " + path); 
    } finally { 
     ctx.setVariableMapper(orig); 
    } 
} 
} 

回答

1

你正在做一個概念性的錯誤。 HTTP響應編寫器是爲了編寫HTML代碼而不是編寫JSF代碼。 webbrowser即只瞭解HTML,而不是JSF。所有常規的JSF組件和渲染器也只是將HTML代碼寫入響應寫入器。在web瀏覽器中打開一個普通的JSF頁面,然後點擊右鍵,然後點擊查看源代碼。如果JSF完成了它的工作,你會發現它是一個和所有的HTML,完全沒有任何JSF代碼。

本質上,您需要創建Facelets標記處理程序而不是JSF UI組件,以便使用基於XML源的新JSF組件處理JSF組件樹。

以下問題的答案包含Hello World標記處理程序。這必須讓你開始:Custom Facelet component in JSF

+0

我們是否創建一個新的TagHandler並讀取xml文件內容來評估內容? – user679526

+0

是的,這是正確的。考慮使用'FaceletContext#includeFacelet()'。另請參見'IncludeHandler'類的''的源代碼。 – BalusC

+0

我改變了上述問題。添加了一個調用facelet的taghandler。這不能解決評估ui:片段代碼的問題。 – user679526