每个包含的模板的单独控制器实例

时间:2019-06-19 13:30:37

标签: jsf jsf-2

我有一个模板panel.xhtml,其中包含n次(n仅在运行时才知道)。

panel.xhtml

...
<c:when test="#{condition}">
    <!-- include other panels -->
    <ui:include src="panel.xhtml">
        <ui:param name="panel" value="#{otherPanel}"/>
    </ui:include>
</c:when>
</c:otherwise>
    <!-- render the content of the panel -->
<c:otherwise>
...

对于每个panel.xhtml,我想要一个控制器类PanelController的新实例。

PanelController.java

@ViewScoped
@ManagedBean(name = "panelCtrl", eager = true)
public final class PanelController {
  private Panel panel;

  @PostConstruct
  public void initialise(Panel panel)
  { 
       panel = (Panel) ((FaceletContext) FacesContext.getCurrentInstance().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY)).getAttribute("panel");
  }
}

注入了ui:param(或通过PostConstruct方法获得-现在是这样)。

问题在于我的控制器只有一个实例,其中的this.panel始终是最后包含的模板的参数。我了解控制器是基于视图的,并且每个请求都会创建一次(大致而言)。如果发生这种情况,@IncludeScoped将是解决方案。

我对现在正在发生的事情以及我想要得到的东西做了一点表示。

现在

enter image description here

我在寻找什么

enter image description here

我正在考虑自定义范围@IncludeViewScoped。对我的情况明智吗?

0 个答案:

没有答案