Cha*_*laa 2 jsf redirect prerenderview jsf-2.2
我终于在页面之间传递了消息,但这并没有将我重定向到用户登录页面(../../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";
}
Run Code Online (Sandbox Code Playgroud)
当然,限制页面有这个:
<f:event type="preRenderView" listener="#{userloginMB.permission()}"/>
Run Code Online (Sandbox Code Playgroud)
使用get external context重定向会使消息丢失.
忽略一般设计问题(在这里看一下起点),似乎你混淆了新的JSF 2.2 <f:viewAction>和旧的JSF 2.0/2.1 <f:event type="preRenderView">技巧.
String仅在GET请求中返回导航结果<f:viewAction>,而不是在<f:event type="preRenderView">.对于后者,你需要ExternalContext#redirect()你曾经发现过的.
所以,你应该这样做
<f:event type="preRenderView" listener="#{bean.onload}"/>
Run Code Online (Sandbox Code Playgroud)
public void onload() throws IOException {
// ...
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}
Run Code Online (Sandbox Code Playgroud)
要么
<f:metadata>
<f:viewAction action="#{bean.onload}"/>
</f:metadata>
Run Code Online (Sandbox Code Playgroud)
public String onload() {
// ...
return "/index.xhtml"; // Note: no need for faces-redirect=true!
}
Run Code Online (Sandbox Code Playgroud)
而不是混淆它们.
请注意,我以这样的方式编写代码,以便您始终可以使用/pathWeb根的相对而不需要弄乱../废话.
| 归档时间: |
|
| 查看次数: |
1434 次 |
| 最近记录: |