【问题标题】:Change body content of custom component更改自定义组件的正文内容
【发布时间】:2023-04-09 07:44:01
【问题描述】:

我想实现一个自定义 jsf 标记,它将获取开始和结束标记之间的文本,对其执行操作,然后返回它。例如,

<l:lessInline>
.list-results {
    .search-result {
        margin-top: 10px;
        margin-bottom: 10px;
    }
}
</l:lessInline>

我需要这个来获取&lt;l:lessInline&gt; 的内容并将其传递给LESS 编译器,然后在&lt;style&gt; 标记之间进行渲染。

我创建了一个 JSF 2.2 FacesComponent,它成功地在 &lt;style&gt; 标签内呈现 Hello World:

@FacesComponent (value = "inlineLess")
public class InlineLessComponent extends UIComponentBase
{

    @Override
    public String getFamily ()
    {
        return "inlineLessComponent";
    }

    @Override
    public void encodeBegin (final FacesContext context) throws IOException
    {
        final ResponseWriter writer = context.getResponseWriter ();
        writer.startElement ("style", this);
        writer.write ("Hello World");
        writer.endElement ("style");
        writer.flush ();
    }

}

我无法弄清楚的是如何“阅读”然后替换标签内的内容。目前,组件标签“内部”的任何内容都保留在渲染元素下方。感谢您的帮助!

【问题讨论】:

    标签: jsf custom-component


    【解决方案1】:

    encodeChildren() 期间渲染主体。只需将响应编写器临时替换为如下所示的本地缓冲区,这样您就可以捕获呈现的输出,而无需将其写入。

    ResponseWriter writer = context.getResponseWriter();
    StringWriter output = new StringWriter();
    context.setResponseWriter(writer.cloneWithWriter(output));
    
    try {
        super.encodeChildren(context);
    }
    finally {
        context.setResponseWriter(writer);
    }
    
    String body = output.toString();
    // ... Manipulate it here if necessary.
    writer.write(manipulatedBody);
    

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多