JSF valuechangelistener not requested when applying

I looked at other similar posts, but none of them helped. Here is my code:

<script type="text/javascript"> function submit1() { document.forms["form"].submit(); } </script> <form name="form"> <h:selectOneMenu value="#{bean.categorySelected}" onchange="submit1()" valueChangeListener="#{bean.changeCategory}"> <f:selectItem itemLabel="Select a Category" itemValue="null" /> <f:selectItems value="#{bean.items}" /> </h:selectOneMenu> </form> 

When I select from the drop-down list, it only reloads the component. This means that bean.getItems is called, but not the value or value of the ChangeListener. My logs indicate that it passed only phase 1 and 6. If I remember correctly, phase 2-5 does not work if the form is not submitted. What am I doing wrong?

+4
source share
1 answer

You need a full-fledged JSF component <h:form> instead of a simple HTML <form> element. A simple HTML <form> element without any method and action attributes is triggered when a default GET request is sent to the current URL. This accurately confirms the behavior you see. It looks like you are just updating the requested page on F5.

So fix it like this:

 <h:form> <h:selectOneMenu value="#{bean.categorySelected}" onchange="this.form.submit()" valueChangeListener="#{bean.changeCategory}"> <f:selectItem itemLabel="Select a Category" itemValue="null" /> <f:selectItems value="#{bean.items}" /> </h:selectOneMenu> </h:form> 

(note that additional JS function is not needed)

<h:form> will generate a <form method="post"> along with at least two hidden JSF input fields identifying the current form and view state (check the difference in the generated HTML output after correcting the JSF code). The combination of a POST request and two hidden fields allows the JSF to know what kind of view it should deal with, what form it should handle, and what listeners and actions it should reference.

+3
source

Source: https://habr.com/ru/post/1392533/


All Articles