3

プロジェクトのコンテキスト

2 つの主要なコンポーネントで URL 書き換えをゼロから作成しました。

public class URLFilter implements Filter
{
    ...
}

public class URLViewHandler extends GlobalResourcesViewHandler
{
    ...
}

最初のクラスは、ページごとに異なる ID を持つ適切なビューにクリーンなURL を転送するために使用されます。2 番目のクラスは関数をオーバーライドして 、ajax 機能が引き続き機能するgetActionURL()ようにします。h:form

これらのクラスは次のように変換されます。

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&amp;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 に正しい軌道に乗せていただき、ありがとうございます。少額の寄付を送りました:)

4

1 に答える 1

9

これは (暗黙の) ナビゲーションでは不可能です。/残念ながら、 は有効な JSF ビュー ID ではありません。

ExternalContext#redirect()代わりに使用してください。交換

action="page.jspx?key=1&amp;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);
}
于 2012-11-19T12:02:04.940 に答える