【发布时间】:2011-11-01 19:52:14
【问题描述】:
当我使用命令按钮重定向到页面时,在我的项目中,你只需要给出不带扩展名的页面名称,然后在 action 属性中添加 ?faces-redirect=true我会被重定向。
但是如果我想被重定向到外部页面(例如:www.google.com)怎么办?
我尝试了很多方法:
www.google.com、google.com、http://google.com
但我失败了。
这就是我所做的:
<h:form>
<h:commandButton
action="#{mainPageBB.goToLink}" value="#{msgs.clickhere}"/>
</h:form>
然后是backing bean:
@Named("mainPageBB")
@RequestScoped
public class MainPageBB {
@EJB
private ILinkManagerEJB linkManagerEJB;
public String goToLink() {
String link = linkManagerEJB.retrieveLink();
if(link != null) {
System.out.println(link);
return link.trim() + "?faces-redirect=true";
}
return null;
}
注意:retrieveLink(); 返回的值始终是 www.google.com(100% 确定)
我在控制台中完全没有错误,页面只是刷新。另外我确信第一个 if 子句验证为 true,所以我认为没有理由跳转到返回 null。
更新
我尝试使用外部上下文,但我得到一个 404,因为它将当前 url 附加到链接字符串:
public String goToRandomLink() {
String link = linkManagerEJB.retrieveRandomLink();
if(link != null) {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
try {
externalContext.redirect(link.trim());
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
【问题讨论】:
标签: java jsf jakarta-ee jsf-2 java-ee-6