2012-02-28 31 views
1

我在我的bean中有一個preRender視圖事件,並且我在用戶中對它進行了一些驗證,並且當出現某種情況時,我使用prettyFaces將用戶重定向到登錄頁面,但重定向似乎不工作,我不知道爲什麼,這裏的代碼:PrettyFaces重定向不適用於preRenderView事件

JSF:

<f:event type="preRenderView" listener="#{myBean.preRender}" /> 

託管Bean:

public String preRender() { 
     log.debug("preRender myPage for user " + userId); 
     try { 
      User user = userService.getUserById(userId); 
      if (!user.isSomeCondition()) { 
       log.debug("Bad Condition"); 
       return "pretty:login"; 
      } 
     } catch (Exception e) { 
      log.error("Error in preRender myPage for user " 
        + userId); 
      return "pretty:login"; 
     } 

     return null; 
    } 

回答

6

您不能返回導航行爲中的字符串離子監聽器方法。它將被完全忽略。只有在<h:commandXxx action="...">提供的實際行動方法中才有可能。

你可以做什麼,而不是手動調用NavigationHandler#handleNavigation()

FacesContext context = FacesContext.getCurrentInstance(); 
NavigationHandler navigator = context.getApplication().getNavigationHandler(); 
navigator.handleNavigation(context, null, "pretty:login"); 
+0

你是對的:) – Lincoln 2012-02-29 05:19:14

+0

@BalusC,如何在preRender中導航,如果不使用漂亮的表面? – 2012-10-20 13:55:39

+0

@Mah:同樣的方法。只需使用像「」login.xhtml「'而不是'」pretty:login「'的正常結果值。或者,只需使用'ExternalContext#redirect()'。 – BalusC 2012-10-20 14:01:52

相關問題