Search |
||
Redirecting from a JSF 2.0 Ajax RequestPosted by driscoll on May 14, 2009 at 7:04 PM PDT
Somewhat recently, I had a user of JSF 2.0 ask how to do a redirect from a JSF 2.0 Ajax request. Here's one way, that I've lifted from one of our tests:
First, the bean that does the work:
Hopefully, I don't have to explain what this is doing - it's sending a redirect request - regardless of whether it's an Ajax request or not. Next, the code that's calling it:
So, what's happening here? Well, once the bean sends the redirect, the ajax client receives a message from the server telling the client to redirect to a new page - all invisibly to the user. In case you're curious, here's what's going back to the client along the wire:
So, a simple ajax request can redirect you to a new page. Like most of the new JSF 2 stuff, we've tried to make things "just work" as much as possible. A little more edgecase than some of my previous blogs perhaps, but probably a useful trick to keep in mind. The code was originally written by Ryan Lubke, BTW, just to make sure that credit lands where it's due. Now that our big push for JavaOne is wrapping up, I'm hoping to have more time to blog in the immediate future. Stay tuned. »
Related Topics >>
Web Applications Comments
Comments are listed in date ascending order (oldest first)
Submitted by cayhorstmann on Fri, 2009-05-15 07:40.
It would be nicer if your RedirectBean didn't have the target URL baked in. Could you use an actionListener and an f:param tag instead?
Use the navigation handler
Submitted by mojavelinux on Sun, 2009-11-08 22:50.
Actually, you can rely on declarative navigation here:
<h:commandButton action="#{bean.action}" value="Invoke and redirect"> <f:ajax execute="@this"/> </h:commandButton> <navigation-rules> <navigation-rule> <from-view-id>/form.xhtml</from-view-id> <navigation-case> <from-action>#{bean.action}</from-action> <from-outcome>success</from-outcome> <to-view-id>/target.xhtml</to-view-id> <redirect/> </navigation-case> </navigation-rule> </navigation-rules> @RequestScoped @ManagedBean public class Bean { public String action() { return "success"; } }
Submitted by driscoll on Fri, 2009-05-15 09:29.
Yes, that's certainly possible. You could also use a valueChange listener operating on a Menu of pages as well - which would be a nifty demo, actually. Let me write that one.
This blog was just to demonstrate the technique, not a best practice.
Submitted by driscoll on Fri, 2009-05-15 11:42.
Actually, just wrote another demo that's bundled into Mojarra's basic-ajax demo, illustrating my above comment (selectOneMenu, valueChangeListener, redirect).
Thought about posting it as another blog, but since even that doesn't really represent best practice (after all, if you're going to do *nothing* but redirect, you probably shouldn't be bothering with an Ajax request), I've decided against it.
But to reiterate - you can put this code into any ajax-called method, not just one with a typically navigation outcome, and it will redirect the page.
How about add f:param inner h:commandButton
Submitted by yyq2009 on Fri, 2009-12-18 19:04.
If I added f:param inner h:commandButton, then, how can I pass the param value to the redirect page?
|
||
|