【问题标题】:faces component content which contains jsf components is not evaluated不评估包含 jsf 组件的 faces 组件内容
【发布时间】:2013-08-23 15:03:28
【问题描述】:

创建了一个 TagHandler 并添加了一个 facelet。 facelet 内容仍未评估。 html 代码包含 ui:fragment 文本。

    @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);
    }
 }
 }

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    你犯了一个概念上的错误。 HTTP 响应编写器旨在编写 HTML 代码,而不是编写 JSF 代码。浏览器即只理解 HTML,不理解 JSF。所有常规的 JSF 组件和渲染器也只是将 HTML 代码写入响应编写器。在 webbrowser 中打开一个普通的 JSF 页面,然后右键单击并查看源代码。如果 JSF 正确地完成了它的工作,您会发现它是一个完整的 HTML,完全没有任何 JSF 代码。

    基本上,您需要创建一个 Facelets 标记处理程序而不是 JSF UI 组件,以便使用基于 XML 源的新 JSF 组件来操作 JSF 组件树。

    以下问题的答案包含一个 Hello World 标记处理程序。这必须让你开始:Custom Facelet component in JSF

    【讨论】:

    • 我们是否新建一个TagHandler并读取xml文件内容来评估内容?
    • 是的,没错。考虑使用FaceletContext#includeFacelet()。另请参阅&lt;ui:include&gt; 的源代码,IncludeHandler 类。
    • 我已经改变了上面的问题。添加了一个调用 facelet 的标记处理程序。这不能解决评估 ui:fragment 代码的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多