Search code examples
jsf-2

JSF Ajax called only 1st time


In JSF2 I have an XHTML like this:

<h:form id="myForm">
...
    <c:forEach items="#{myController.header}" var="hd" varStatus="count">
         #{hd}
         <h:selectOneMenu value="#{myController.type[count.index]}" id="formtype" >
                 <f:selectItem itemValue="Nop" itemLabel="Nop" />
                 <f:selectItem itemValue="Yep" itemLabel="Yep" />
                 <f:ajax render="@form" />
          </h:selectOneMenu>
          #{myController.type[count.index]}
          <h:selectBooleanCheckbox value="#myController.valid[count.index]}" id="valid">
                 <f:ajax render="@form"/>
          </h:selectBooleanCheckbox> 
          #{myController.valid[count.index]}
     </c:forEach>
...
</h:form>

And my managed bean has:

private String[] type;
private boolean[] valid;

which I initialise (type = new String[...]; and assign a value to each position) when the named bean is 1st loaded.

If I modify the combo or the check box, it works, the value is changed in the managed bean and the new value gets printed (rendered) in the JSF page. But only the 1st time. Next changes do not modify anything at all. It is strange because I have similar code in other apps and works fine, so I am doing something incorrectly but cannot find it. My template has a h:header and I cannot see any logs in the JS console or in TomEE (I only see .INFO - The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/myApp]] after start() had already been called. The second call will be ignored but I think it is unrelated). I have tried to add execute="@this"/execute="@form" and others unsuccessfully, and check all points of this post but nothing seems to apply to me.

What is the mistake I am not seeing?


EDIT

@Named(value = "myController")
@ViewScoped
public class MyController implements Serializable {
...

I also have a <h:messages globalOnly="true" showDetail="true" showSummary="true" /> in my template.


Solution

  • I think I got it, finally!

    Thanks to two things:

    1. Point 3 in BalusC answer. I added an id to my message in the template <h:messages id="msg" and then use <f:ajax render="@form msg"..... This made an error j_idt1: Validation Error: Value is required to appear.

    2. Then I could find the solution in this post: to change the required parameter from true to <f:viewParam required="#{!facesContext.postback}". This was not required in my other apps because required was "false", since it could be called with parameter or without it. When it was called with a parameter, it worked because by serendipity the page could be called without it.