Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

jsf - Primefaces onclick and onsuccess differences

I have the following situation:

after clicking a button, some business logic is done and after it is done, a new tab with a report should be visible.

<p:commandButton value="this button" update="growlMain"
                 actionListener="#{myBean.businesslogic}" 
                 onstart="ajaxDialog.show();"
                 oncomplete="ajaxDialog.hide();"
                 onsuccess="window.open('./report.jsp', '_newtab');" />

This does not work :(

If the business logic only lasts some milliseconds, the following works:

<p:commandButton value="this button" update="growlMain"
                 actionListener="#{myBean.fastbusinesslogic}" 
                 onclick="window.open('./report.jsp', '_newtab');" />

the onclick opens a new tab, also things like onstart but it doesn't work with onsuccess or oncomplete. Why? And is there a solution for business logic that lasts some seconds?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

onclick is called before the ajax request is even created (pure client side) while oncomplete and onsuccess are executed after the server responded to the ajax request. So, if you need to execute some business logic before showing a dialog, for example, you want to go with oncomplete. That's what I always use.

You can also condition your javascript inside oncomplete to perform only if there's no validation errors. Intuitively I think onsuccess would behave like that and only execute when there's no validation errors, but that's not how it goes. I don't really know the difference between them. I assume there's a way to flag success=false in the backing beans, but I couldn't really find it in the documentation.

If you want to check for validation in your oncomplete attribute, this is how you'd do:

oncomplete="if (!args.validationFailed){someDialog.hide()}"

In this case, you would only close the dialog if the fields are properly validated. You can actually inject parameters from the backing bean and use them in the javascript after the request has been served. In your backing bean you can do something like this:

RequestContext.getCurrentInstance().addCallbackParam("showDialog", false);

And you can access the parameter like this in your incomplete attribute:

oncomplete="if (args &amp;&amp; args.showDialog){someDialog.show()}else{ alert('the bean didnt let me open the dialog')}"

Anyway, I hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...