Cancel button does not work in case of verification error

I have a form with some inputs that have validation (required = true) and when I click the cancel button in case of a validation error, the cancel button does not go to the previous page, instead it removes the validation error (I think it returns to one the step that was before the validation error?)

here is my code:

<h:form> <h:inputText value="#{myBean.nickName}" id="nickname" required="true" requiredMessage="nickname should be specified" /> <h:commandLink immediate="true" id="cancel_link" onclick="history.back(); return false" style="float: left;margin: 118px 189px 0 0;"> <h:graphicImage width="90" height="28" value="#{resource['images:blu_btnCancel.png']}" /> </h:commandLink> </h:form> 

please advise how to fix it.

0
source share
1 answer

The JavaScript history.back() function returns you to the previous synchronous request, and not to the previous view, as you expected.

Although history.back() is horrible in this way (unreliable, hackable, etc.), a quick solution would be to send an ajax request to submit submit instead of a synchronous request.

 <h:form> ... <h:commandButton value="submit"> <f:ajax execute="@form" render="@form" /> </h:commandButton> </h:form> 

Ajax requests are not counted as browser history.

A more reliable way is to simply pass the identifier of the previous view during navigation, and then use <h:link> to link to it. For instance.

 <h:link value="Go to next view" outcome="nextview"> <f:param name="from" value="#{view.viewId}" /> <h:link> 

And then in nextview.xhtml

 <f:metadata> <f:viewParam name="from" /> </f:metadata> ... <h:link ... outcome="#{from}" rendered="#{not empty from}"> <h:graphicImage ... /> </h:link> 

If you are navigating POST, you might consider using flash memory to remember the initial look.


Unrelated to the specific problem, the correct way to use <h:graphicImage> with JSF2 resources is to simply use your name attribute instead of your value attribute with #{resource} , which is just awkward.

Replace

 <h:graphicImage width="90" height="28" value="#{resource['images:blu_btnCancel.png']}" /> 

by

 <h:graphicImage name="images/blu_btnCancel.png" width="90" height="28" /> 

Note that the name of the images library here is simply replaced by the path name. Using the name "image" as the name of the library is very doubtful. See Also What is a JSF Resource Library and How to Use It?

+2
source

All Articles