0

I have a list of different objects, which should get rendered each in it's own way based by it's type (eg. String = HtmlInputText, Text = HtmlInputText, ...). Based on one of the Answers in StackOverflow I decided to do this via binding. Currently I have the following construct:

...
<h:head>
<h:outputScript library="javax.faces" name="jsf.js" target="head" />
</h:head>
<h:body>
<h:form id="form">
    <h:panelGrid binding="#{componentBean.grid}">

    </h:panelGrid>
</h:form>
</h:body>
</html>

My ComponentBean is a request scoped bean (because of the binding) which just holds the binded Grid and sets the list of components. private HtmlPanelGrid grid:

@Inject
private EntityBean entityBean;

@PostConstruct
public void init() {
    grid = new HtmlPanelGrid();
    initComponents();
}

    public void initComponents() {
    for (Slot slot : entityBean.getEntity().getSlots()) {
        if (slot.getType().getTypeClass().equals(TypeClass.ForeignKey))
            grid.getChildren().add(entityBean.getComponent(slot.getID()));
    }
}
...

And my thought was that I have a second bean called EntityBean which holds the created UIComponents in a list, so they don't get reinitialized each request. This works pretty well so far. My Problem now is, that regarding on the actions (eg. a SelectOneMenu has changed in a certain way) I have to redraw the html components in my request scoped ComponentBean. I used Ajax in the selectOneMenu to do my action (eg. removing one component from the list). Now the problem is that my ComponentBean init() is getting called BEFORE my actionListener is called, so the components get redrawn before the action happens. I want the action to happen and redraw the component afterwards. Does anyone have a clue about what I could possibly do here?

user3172567
  • 461
  • 1
  • 7
  • 19
  • The duplicate explains exactly how `binding` works and how it should be used. At least, JSF needs the component tree in order to collect submitted input values and identify the invoked action. I personally recommend you to just use XHTML+XML instead of Java to define the component tree structure. You will end up with so much more intuitive and self-documenting code. – BalusC Sep 07 '16 at 07:21
  • Hi, thanks for your tag and answer. I thought about doing this the way I did because I have things like chained SelectOneMenus depending on each other which I thought would be easier this way. Do you recommend doing it via XHTML + XML? Is it even possible to do things like described (chaining SelectOneMenus) in this way? Or is it more common to do it via JSTL? Don't really have a clue how best practice is – user3172567 Sep 07 '16 at 07:26
  • That's already answered in abovelinked duplicate and also here http://stackoverflow.com/q/3510614 The specific example of cascading dropdowns can be found here http://stackoverflow.com/q/16378099 – BalusC Sep 07 '16 at 07:27
  • I've already seen this post, this is where I decided to do it the way I did. I didn't read anything about the possibilities of chaining components which is an important feature for my needs. – user3172567 Sep 07 '16 at 07:31
  • The given links clearly tell that XHTML+XML and Java have same capabilities. Only with Java code you end up with much more verbose/brittle/confusing/hard-to-maintain code. – BalusC Sep 07 '16 at 07:33
  • Ok thanks BalusC. I'll give it a try since the Java-Way seems too complicated. You're the best :) – user3172567 Sep 07 '16 at 07:34

0 Answers0