项目上下文
我用两个主要组件从头开始创建了一个URL重写:
public class URLFilter implements Filter
{
...
}
public class URLViewHandler extends GlobalResourcesViewHandler
{
...
}
第一个类用于将干净的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;" />
我的惠斯
我的问题是:有没有更好的方法来编程注销按钮,因为我需要重定向到上下文根,目前我使用的是带有主页键的视图名称,但我更喜欢1。使用真实路径2。不保留?在url处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让我走上正轨!发送了一笔小额捐款:)
这在(隐式)导航中是不可能的。不幸的是,/
不是一个有效的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);
}