1
我有一个自定义容器组件,我想用这样的:如何获取自定义组件的父级?
<p:a>A
<p:a>B</p:a>
</p:a>
这应该产生这样的标记:
<div>A
<div>B</div>
</div>
代码组件如下。
public class TagA extends TagHandler {
Logger logger = Logger.getLogger(getClass().getName());
public TagA(TagConfig config) {
super(config);
}
public void apply(FaceletContext ctx, UIComponent parent)
throws IOException {
UIComponentBase c = new UIComponentBase() {
public void encodeBegin(FacesContext ctx) throws IOException {
//getParent() always returns UIViewRot
logger.info("Parent is: " + getParent().getClass().getName());
ResponseWriter w = ctx.getResponseWriter();
w.write("<div>");
super.encodeBegin(ctx);
}
public void encodeEnd(FacesContext ctx) throws IOException {
ResponseWriter w = ctx.getResponseWriter();
w.write("</div>");
super.encodeEnd(ctx);
}
// abstract method in base, must override
public String getFamily() {
return "com.mobiarch.nf";
}
};
parent.getChildren().add(c);
nextHandler.apply(ctx, parent);
}
}
不幸的是,这是呈现以下标记:
<div></div>A
<div></div>B
这种做法实际上是 “跆拳道?”。你为什么不使用普通的'UIComponent'? – BalusC
谢谢首席。我正在关注一些糟糕的教程。我会在这里发布我的解决方案。 – RajV