Mixing Ajax and full requests in JSF 2.0
The JSF code which is giving me problems is the following:
<h:panelGrid columns="3">
<!-- Minimum Password Length -->
<h:outputText value="#{i18n开发者_Python百科['xxx']}:" />
<h:inputText id="minLength"
value="#{passwordPolicies.minLength.paramValue}"
required="true">
<f:validateLongRange minimum="1"/>
<f:ajax event="valueChange"
render="@this minLengthMessage"
listener="passwordPolicies.testListener"/>
</h:inputText>
<h:message id="minLengthMessage"
for="minLength"
errorClass="error"
tooltip="true"/-->
<!-- Many other validation fields -->
<h:panelGrid/>
Firstly, I am having a very similar issue as: f:ajax listener never fired . I must combine Ajax and full requests on my JSF page. Jim Driscoll @ java.net says that in order for these 2 to work, an Ajax error listener must be set up? Is this very much diferent than the listener property of the ajax tag?
Now, I know I could try Primefaces p:inputText and p:ajax tags, but these would require me individual listeners for all my fields that are validated. Is there a way to fix the f:ajax error listener, in order to be triggered and dont get the nasty:
Thanks for any input!
You need to make your command button to fire an ajax request instead.
<h:commandButton value="Submit" action="#{bean.submit}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
Those <f:ajax>
listeners are not mandatory. They were just used as an example in Jim's article to illustrate the race condition. Which get invoked first? Obviously you'd like to invoke the listeners first.
A practical example of almost the same kind of form can be found in this chapter of this tutorial. Only blur
is been used instead of valueChange
, because the valueChange
won't be invoked when you blur a required="true"
field which is kept empty which causes that the "Value is required" message will never show up on such a field.
Just recording this here for posterity, but creating an error handler in JavaScript and then referencing it from the f:ajax
tag will suppress the error alert and cause your error handler to be called with the error message instead. This behavior is described in the article by Jim Driscoll mentioned in the question but not specifically demonstrated. I've verified that it works:
function onAjaxError( error ) { console.log( error.description ); }
<f:ajax onerror="onAjaxError" ...>
if you want to add some monitor this is how :
<h:commandButton value="Submit" action="#{yourBean.YourAction}" render="thisIsWhereYouWantToDisplay">
<f:ajax onevent="monitor" execute="@form" render="@form" />
</h:commandButton>
精彩评论