项目背景
我从头开始创建了一个带有两个主要组件的URL重写:
public class URLFilter implements Filter
{
...
}
public class URLViewHandler extends GlobalResourcesViewHandler
{
...
}
第一个类用于将 clean URL转发到右侧视图,每个页面的ID不同。第二个类重写函数getActionURL()
,以便h:form
和ajax函数继续有效。
这些课程的翻译方式如下:
Real URL Internal URL
/ <-> page.jspx?key=1
/contact <-> page.jspx?key=2
/projects/management <-> page.jspx?key=3
etc
当前解决方案
我现在的问题是我的用户登录和退出按钮:
<!-- Login button used if user is not logged, go to a secured page (which display error message). If he log with this button, the current page is reloaded and displayed properly. This button works perfectly -->
<h:commandButton rendered="#{pageActions.item.isPrivate}" value="#{msg.button_connect}" actionListener="#{userActions.onButtonLoginClick}" />
<!-- Login button used anywhere on public pages that redirect to user home after login, works perfectly since I haven't changed to clear url. -->
<h:commandButton rendered="#{not pageActions.item.isPrivate}" value="#{msg.button_connect}" actionListener="#{userActions.onButtonLoginClick}" action="userHome.jspx?faces-redirect=true" />
<!-- Logout button that works (it redirects at http://website.com/context-name/ but keep the ?key=1 at the end. -->
<h:commandButton value="#{msg.button_disconnect}" actionListener="#{userActions.onButtonLogoutClick}" action="page.jspx?key=1&faces-redirect=true" styleClass="button" style="margin-left: 5px;" />
我的嘘声
我的问题:有没有更好的方法来编写注销按钮,因为我需要重定向到context-root,目前我正在使用带有主页键的视图名称,但我更喜欢1.使用真实路径2 。不要在网址上保留?key = 1。
谢谢!
最终代码
根据BalusC的回答,这是我与其他人分享的最终代码:
@ManagedBean
@RequestScoped
public class NavigationActions
{
public void redirectTo(String p_sPath) throws IOException
{
ExternalContext oContext = FacesContext.getCurrentInstance().getExternalContext();
oContext.redirect(oContext.getRequestContextPath() + p_sPath);
}
}
<h:commandButton rendered="#{not pageActions.item.isPrivate}" value="#{msg.button_connect}" actionListener="#{userActions.onButtonLoginClick}" action="#{navigationActions.redirectTo(userSession.language.code eq 'fr' ? '/profil/accueil' : '/profile/home')}" />
因为我有路径时不需要钥匙,所以更好,再次感谢BalusC让我走上正轨!发送了一笔小额捐款:)
答案 0 :(得分:7)
(隐式)导航无法做到这一点。遗憾的是,/
不是有效的JSF视图ID。
请改用ExternalContext#redirect()
。取代
action="page.jspx?key=1&faces-redirect=true"
通过
action="#{userActions.redirectToRootWithKey(1)}"
与
public void redirectToRootWithKey(int key) throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "?key=" + key);
}