Jsf 在页面上单击CommandButton动态实例化复合组件
下面是传奇人物巴卢斯克对此帖子的回复: 如果我有足够的观点,我会在那篇文章上加上评论——但我没有足够的观点 问题如下 我试图设置一个commandButton,将JSF复合组件动态添加到xhtml文件中。目的是多次单击它将在页面上放置该组件的多个实例 我在XHTML文件上有一个按钮:Jsf 在页面上单击CommandButton动态实例化复合组件,jsf,jsf-2.2,composite-component,Jsf,Jsf 2.2,Composite Component,下面是传奇人物巴卢斯克对此帖子的回复: 如果我有足够的观点,我会在那篇文章上加上评论——但我没有足够的观点 问题如下 我试图设置一个commandButton,将JSF复合组件动态添加到xhtml文件中。目的是多次单击它将在页面上放置该组件的多个实例 我在XHTML文件上有一个按钮: <h:commandButton action="#{assessingController.addQuestion}" value="Add a Question"></h:command
<h:commandButton action="#{assessingController.addQuestion}" value="Add a Question"></h:commandButton>
UIComponent parent=null代码>--因为在传递到includeCompositeComponent
之前,必须以某种方式实例化或引用它。但是正如下面提到的,将其设为null可能会导致null指针异常(那么我应该怎么做呢?)
includeCompositeComponent
方法与BalusC在上述文章中提到的JSF 2.2方法相同:
public void includeCompositeComponent(UIComponent parent, String taglibURI, String tagName, String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIComponent composite = context.getApplication().getViewHandler()
.getViewDeclarationLanguage(context, context.getViewRoot().getViewId())
.createComponent(context, taglibURI, tagName, null);
composite.setId(id);
parent.getChildren().add(composite);
}
单击commandButton时,日志如下所示:
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315) ...
Caused by: java.lang.NullPointerException
at controllers.AssessingController.includeCompositeComponent(AssessingController.java:123)
AssessingController.java:123是这一行:
parent.getChildren().add(composite);
Composite不为null(已选中)。
很明显,-parent是空的,这就是问题所在
那么,我如何才能更好地引用UIComponent
parent呢?
我需要让它引用xhtml文件中的某些内容吗?我想它需要某种占位符,作为父对象(?)。现在,xhtml页面上只有commandButton
谢谢大家 父级
应该表示您希望在其中包含复合组件的组件
想象一下,您最终希望得到这个简单的XHTML表示:
<h:panelGroup id="questions">
<your:questionComposite />
</h:panelGroup>
或者,通过混凝土构件本身:
<h:form>
<h:commandButton ... action="#{bean.addQuestion(questions)}" />
</h:form>
<h:panelGroup id="questions" binding="#{questions}" />
与具体问题无关:这里有一个思维/设计错误。您应该使用
,然后从@ViewScoped
bean向
馈送一个表示组合模型值的动态大小的实体列表
<h:form>
<h:commandButton ... action="#{bean.addQuestion}" />
</h:form>
<ui:repeat value="#{bean.questions}" var="question">
<your:questionComposite value="#{question}" />
</ui:repeat>
私人列表问题;
公共问题{
添加(新问题());
}
任何时候,当您需要处理支持bean中的原始UIComponent
实例时,请暂停一下,仔细研究或询问您是否真的以正确的方式做事。也许它属于一个支持组件,或者可以用纯XHTML来完成
另见:
public void addQuestion() {
UIComponent parent = context.getViewRoot().findComponent("questions");
// ...
}
<h:form>
<h:commandButton ... action="#{bean.addQuestion(questions)}" />
</h:form>
<h:panelGroup id="questions" binding="#{questions}" />
public void addQuestion(UIComponent parent) {
// ...
}
<h:form>
<h:commandButton ... action="#{bean.addQuestion}" />
</h:form>
<ui:repeat value="#{bean.questions}" var="question">
<your:questionComposite value="#{question}" />
</ui:repeat>
private List<Question> questions;
public void addQuestion() {
questions.add(new Question());
}