2012-11-19 2 views
3

프로젝트 컨텍스트h : commandButton 액션을 컨텍스트 루트로 리디렉션 할 수 있습니까?

나는 처음부터 두 가지 주요 구성 요소로 재 작성 URL을 만들었습니다

public class URLFilter implements Filter 
{ 
    ... 
} 

public class URLViewHandler extends GlobalResourcesViewHandler 
{ 
    ... 
} 

첫 번째 클래스는 다른 ID로 올바른보기로 깨끗한 URL을 전달하는 데 사용됩니다 각 페이지마다. 두 번째 클래스는 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&amp;faces-redirect=true" styleClass="button" style="margin-left: 5px;" /> 

내 whishes

내 질문 : 컨텍스트 루트로 리디렉션해야하므로 로그 아웃 버튼을 프로그래밍하는 더 좋은 방법이 있습니까? 현재 홈 페이지 키와 함께 뷰 이름을 사용하고 있지만 실제 경로 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 다시 올바른 트랙에 나를 넣어 주셔서 감사합니다! 작은 기부를 보냈습니다.

+0

action="#{userActions.redirectToRootWithKey(1)}" 

에 의해

action="page.jspx?key=1&amp;faces-redirect=true" 

교체 난 당신이 질문/문제를 이해하지 않습니다. faces-config.xml에서 탐색을 시도 했습니까? logout / roel

+0

@roel, 제안 해 주셔서 감사합니다. 테스트를 마쳤습니다. 작동하지 않습니다. 주요 관심사는 "버튼 동작에 실제 URL을 입력하는 방법"입니다. 나는 action = "/"을 넣길 원하지만 작동하지 않습니다. 또한 탐색 규칙을 사용하고 싶지 않습니다. –

+0

그런데 왜 PrettyFaces와 같은 기존 라이브러리를 재사용하는 대신 URL 재 작성 솔루션을 재발 명하는 이유는 무엇입니까? – BalusC

답변

7

(암시 적) 탐색에서는 불가능합니다. /은 유감스럽게도 유효한 JSF 뷰 ID가 아닙니다.

대신 ExternalContext#redirect()을 사용하십시오.

public void redirectToRootWithKey(int key) throws IOException { 
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); 
    ec.redirect(ec.getRequestContextPath() + "?key=" + key); 
} 
+0

내 CMS로 루프를 닫는 아주 좋은, 정확한 해결책! 고맙습니다! –

+0

P.:: ec.redirect 끝에 괄호가 없습니다. :) –

+0

이미 1 초 전에 고정되어 있습니다 :) – BalusC

관련 문제