[英]Why is OmniFaces Ajax utility's oncomplete() not working when invoked by preRenderView event?
[英]URL redirection not working on preRenderView event
我終於在頁面之間傳遞了消息,但這沒有將我重定向到用戶登錄頁面(../../index.xhtml),而是顯示了禁止頁面:
public String permission() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
String isLog = (String) sessionMap.get("isLogged");
if(isLog != "TRUE") {
System.out.println("*** The user has no permission to visit this page. *** ");
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info : ", "Loggez-vous"));
context.getExternalContext().getFlash().setKeepMessages(true);
//context.getExternalContext().redirect("../../index.xhtml");
return "../../index.xhtml?faces-redirect=true";
} else {
System.out.println("*** The session is still active. User is logged in. *** ");
}
return "../../index.xhtml?faces-redirect=true";
}
當然,受限制的頁面具有以下內容:
<f:event type="preRenderView" listener="#{userloginMB.permission()}"/>
使用獲取外部上下文進行重定向將使消息丟失。
忽略一般的設計問題( 在這里尋找起點),似乎您將新的JSF 2.2 <f:viewAction>
和舊的JSF 2.0 / 2.1 <f:event type="preRenderView">
技巧<f:event type="preRenderView">
。
僅在<f:viewAction>
支持在GET請求上以String
返回導航結果,而在<f:event type="preRenderView">
。 對於后者,您需要恰好已被注釋掉的ExternalContext#redirect()
。
所以,你應該要么
<f:event type="preRenderView" listener="#{bean.onload}"/>
public void onload() throws IOException {
// ...
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}
要么
<f:metadata>
<f:viewAction action="#{bean.onload}"/>
</f:metadata>
public String onload() {
// ...
return "/index.xhtml"; // Note: no need for faces-redirect=true!
}
不要混在一起
請注意,我以這樣的方式編寫了代碼:始終可以使用相對於Web根的/path
,而無需擺弄../
廢話。
使用faces-redirect = true時,需要從根上下文指定絕對路徑 。
因此您的結果字符串應類似於:
return "/dir1/dir2/index.xhtml?faces-redirect=true";
如果index.xhtml駐留在(上下文根)中,即Web Content /index.xhtml,則使用以下結果字符串:
return "/index.xhtml?faces-redirect=true";
如果index.xhtml駐留在Web Content /pages/dir2/index.xhtml中,則使用以下結果字符串:
return "/pages/dir2/index.xhtml?faces-redirect=true";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.