jsf - commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated -
sometimes, when using <h:commandlink>, <h:commandbutton> or <f:ajax>, action, actionlistener or listener method associated tag not being invoked. or, bean properties not updated submitted uiinput values.
what possible causes , solutions this?
introduction
whenever uicommand component (<h:commandxxx>, <p:commandxxx>, etc) fails invoke associated action method, or uiinput component (<h:inputxxx>, <p:inputxxxx>, etc) fails process submitted values and/or update model values, , aren't seeing googlable exceptions and/or warnings in server log, not when configure ajax exception handler per exception handling in jsf ajax requests, nor when set below context parameter in web.xml,
<context-param> <param-name>javax.faces.project_stage</param-name> <param-value>development</param-value> </context-param> and not seeing googlable errors and/or warnings in browser's javascript console (press f12 in chrome/firefox23+/ie9+ open web developer toolset , open console tab), work through below list of possible causes.
possible causes
uicommand,uiinputcomponents must placed insideuiformcomponent, e.g.<h:form>(and not plain html<form>), otherwise nothing can sent server.uicommandcomponents must not havetype="button"attribute, otherwise dead button useful javascriptonclick. see how send form input values , invoke method in jsf bean , <h:commandbutton> not initiate postback.you cannot nest multiple
uiformcomponents in each other. illegal in html. browser behavior unspecified. watch out include files! can useuiformcomponents in parallel, won't process each other during submit. should watch out "god form" antipattern; make sure don't unintentionally process/validate other (invisible) inputs in same form (e.g. having hidden dialog required inputs in same form). see how use <h:form> in jsf page? single form? multiple forms? nested forms?.no
uiinputvalue validation/conversion error should have occurred. can use<h:messages>show messages not shown input-specific<h:message>components. don't forget includeidof<h:messages>in<f:ajax render>, if any, updated on ajax requests. see h:messages not display messages when p:commandbutton pressed.if
uicommandoruiinputcomponents placed inside iterating component<h:datatable>,<ui:repeat>, etc, need ensure samevalueof iterating component been preserved during apply request values phase of form submit request. jsf reiterate on find clicked link/button , submitted input values. putting bean in view scope and/or making sure load data model in@postconstructof bean (and not in getter method!) should fix it. see how , when should load model database h:datatable.the
renderedattribute of component , of parents ,testattribute of parent<c:if>/<c:when>should not evaluatefalseduring apply request values phase of form submit request. jsf recheck part of safeguard against tampered/hacked requests. storing variables responsible condition in@viewscopedbean or making sure you're preinitializing condition in@postconstructof@requestscopedbean should fix it. same appliesdisabledattribute of component, should not evaluatetrueduring apply request values phase. see jsf commandbutton action not invoked , form submit in conditionally rendered component not processed.the
onclickattribute ofuicommandcomponent ,onsubmitattribute ofuiformcomponent should not returnfalseor cause javascript error. there should in case of<h:commandlink>or<f:ajax>no js errors visible in browser's js console. googling exact error message give answer. see adding jquery primefaces results in uncaught typeerror on place.if you're using ajax via jsf 2.x
<f:ajax>or e.g. primefaces<p:commandxxx>, make sure have<h:head>in master template instead of<head>. otherwise jsf won't able auto-include necessary javascript files contains ajax functions. result in javascript error "mojarra not defined" or "primefaces not defined" in browser's js console. see h:commandlink actionlistener not invoked when used f:ajax , ui:repeat.if you're using ajax, make sure
uiinput,uicommandcomponents of interest covered<f:ajax execute>or e.g.<p:commandxxx process>, otherwise won't executed/processed. see submitted form values not updated in model when adding <f:ajax> <h:commandbutton> , understanding primefaces process/update , jsf f:ajax execute/render attributes.if parent of
<h:form>uicommandbutton beforehand been rendered/updated ajax request coming form in same page, first action fail. second , subsequent actions work. caused bug in view state handling reported jsf spec issue 790 , scheduled fixed in jsf 2.3. older jsf versions, need explicitly specify id of<h:form>inrenderof<f:ajax>. see h:commandbutton/h:commandlink not work on first click, works on second click.if
<h:form>hasenctype="multipart/form-data"set in order support file uploading, need make sure you're using @ least jsf 2.2, or servlet filter responsible parsing multipart/form-data requests configured, otherwisefacesservletend getting no request parameters @ , not able apply request values. how configure such filter depends on file upload component being used. tomahawk<t:inputfileupload>, check this answer , primefaces<p:fileupload>, check this answer. or, if you're not uploading file @ all, remove attribute altogether.make sure
actioneventargument ofactionlistenerjavax.faces.event.actionevent, notjava.awt.event.actionevent, ides suggest 1st autocomplete option. having no argument wrong if useactionlistener="#{bean.method}". if don't want argument in method, useactionlistener="#{bean.method()}". or perhaps want useactioninstead ofactionlistener. see differences between action , actionlistener.make sure no
phaselisteneroreventlistenerin request-response chain has changed jsf lifecycle skip invoke action phase example callingfacescontext#renderresponse()orfacescontext#responsecomplete().make sure no
filterorservletin same request-response chain has blocked request fofacesservletsomehow.bug in framework. example, richfaces has "conversion error" when using
rich:calendarui elementdefaultlabelattribute (or, in cases,rich:placeholdersub-element). bug prevents bean method being invoked when no value set calendar date. tracing framework bugs can accomplished starting simple working example , building page until bug discovered.
debugging hints
in case still stucks, it's time debug. in client side, press f12 in webbrowser open web developer toolset. click console tab see javascript conosle. should free of javascript errors. below screenshot example chrome demonstrates case of submitting <f:ajax> enabled button while not having <h:head> declared (as described in point 7 above).
click network tab see http traffic monitor. submit form , investigate if request headers , form data , response body per expectations. below screenshot example chrome demonstrates successful ajax submit of simple form single <h:inputtext> , single <h:commandbutton> <f:ajax execute="@form" render="@form">.
(warning: when post screenshots http request headers above production environment, make sure scramble/obfuscate session cookies in screenshot avoid session hijacking attacks!)
in server side, make sure server started in debug mode. put debug breakpoint in method of jsf component of interest expect called during processing form submit. e.g. in case of uicommand component, uicommand#queueevent() , in case of uiinput component, uiinput#validate(). step through code execution , inspect if flow , variables per expectations. below screenshot example eclipse's debugger.



Comments
Post a Comment