0

I've got a template panel.xhtml which includes itself n times (n will be known only in runtime).

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>
...

For each panel.xhtml I want to have a new instance of the controller class 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");
  }
}

with the ui:param injected (or obtained in a PostConstruct method - as it is now).

The problem is that there is only one instance of my controller and this.panel in it is always the parameter of the last-included template. I understand the controller is view-scoped and is created once per request (roughly speaking). If there was such a thing, @IncludeScoped would be the solution.

I drew a little representation of what it's happening now, and of what I'd like to get.

Now

enter image description here

What I'm looking for

enter image description here

I was thinking of a custom scope @IncludeViewScoped. Would it be sensible for my scenario?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 1
    Seaparate controller instances is not logical. But if you actually want this, do it something like https://stackoverflow.com/questions/28879656/multiple-instances-of-managed-bean – Kukeltje Jun 19 '19 at 13:36
  • _" I understand the controller is view-scoped and is created once per request (roughly speaking). "_: Wrong, once per creation of the view (yes via an http get request) but if you do many ajax requests on the same view will still be created once – Kukeltje Jun 19 '19 at 13:42
  • @Kukeltje thank you for the reply. It might be not logical for someone with JSF background, but it sounds logical to me. I want a panel controller to back up only one panel. If something happened in the panel, this panel will go to its controller, which completely unaware of the existence of others. – Andrew Tobilko Jun 19 '19 at 13:48
  • @Kukeltje read and upvoted your answer there. It's unclear what role `RequestCalculation` has, though. Is it a controller? It looks like there are one "true" view-scoped controller and other smaller "false" controllers. The main one is up to dispatching anything to a specific controller. Is it correct? – Andrew Tobilko Jun 19 '19 at 13:50
  • @Kukeltje I put "roughly speaking" because the scope (request-, view-) doesn't matter to me at this point. – Andrew Tobilko Jun 19 '19 at 13:51
  • 1
    Maybe you actually want to implement a composite component backed by a custom class: https://stackoverflow.com/a/7040731/865107 – Selaron Jun 19 '19 at 14:20

0 Answers0