注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Redirecting from a JSF 2.0 Ajax Request  

2010-01-15 14:04:20|  分类: jsf2.0 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
                    

By Jim Driscoll

Redirecting from a JSF 2.0 Ajax Request

Posted 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: DE<DE<

@ManagedBean  @RequestScoped  public class RedirectBean {        public String redirect() {          FacesContext ctx = FacesContext.getCurrentInstance();          ExternalContext extContext = ctx.getExternalContext();          String url = extContext.encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/ajax/redirecttarget.xhtml"));          try {              extContext.redirect(url);          } catch (IOException ioe) {              throw new FacesException(ioe);          }          return null;       }
DE<

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:

DE<DE<

<h:head>      <title>Ajax Redirect</title>  </h:head>  <h:body>      <h:form id="form">          <h:commandButton id="redirect" value="Redirect" action="#{redirectBean.redirect}">               <f:ajax execute="@this" render="@none"/>          </h:commandButton>      </h:form>  </h:body>  
DE<

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:

DE<DE<

"<?xml version="1.0" encoding="utf-8"?>  <partial-response>    <redirect url="/contextpath/faces/ajax/redirecttarget.xhtml">    </redirect>  </partial-response>"  
DE<

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.

From:http://weblogs.java.net/blog/2009/05/14/redirecting-jsf-20-ajax-request

  评论这张
 
阅读(830)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018