问题描述
我有一个具有以下xhtml结构的JSF 2应用程序:
I have a JSF 2 application with the following xhtml structure:
-webapp --index.xhtml --folder1 --targetPage.xhtml --folder2 --otherPage.xhtml
--webapp --index.xhtml --folder1 --targetPage.xhtml --folder2 --otherPage.xhtml
我有一个名为TargetPageBean的托管bean,使用以下方法:
I have a managed bean called TargetPageBean with the following method:
public String navigateToTargetPage() {
if( isOnIndexPage() ) {
return "folder1/targetPage.xhtml?faces-redirect=true";
} else {
return "targetPage.xhtml?faces-redirect=true";
}
}
这在两种情况下都可以正常工作:
This would work fine in these two cases:
- 我只是从索引页面导航到目标页面
- 我已经在目标页面上,然后再次导航到该页面
但是这种方法非常糟糕,因为我的树结构可能更深,我可能想从几乎所有地方移到目标页面.我可以告诉JSF,它应该始终从根"重定向,以便我只需要返回folder1/targetPage.xhtml?faces-redirect=true
吗? (我不想通过faces-config文件使用显式导航)
But this approach is pretty bad because my tree strcuture could be deeper and I could want to move to the target page from nearly everywhere. Can I tell JSF that it should always redirect from the "root" so that I just have to return folder1/targetPage.xhtml?faces-redirect=true
? (I do not want to use the explicit navigation via the faces-config file)
推荐答案
您可以将重定向与完整的请求路径一起使用:
You can use redirect with full request path:
getFacesContext().getExternalContext().redirect(getRequest().getContextPath() +
"/page.jsf?" + Constants.CONSTANT_NAME + "=" + bean.getSomeValue());
其中
protected HttpServletRequest getRequest() {
return (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
}
protected FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
这篇关于始终从根文件夹进行JSF 2导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!