我已经获得了cookie的价值。但重定向到其他页面方法不起作用。它只返回空白页面。我指的是用户在地址栏上直接访问它。我希望他们重定向到主页而不是他们想要访问的页面..使用我的cookie的值
继承人的片段。
if(regdate before or after endregdate) {create cookie with value something}
else {create cookie with value something}
使用ExternalContext重定向(ec.getrequestcontextpath +“/ homepage.xhtml”);
然后他们尝试访问的我的xhtml有<f:event listener="#{bean.redirectPageMethod}" type="preRenderView">
ADDED SNIPPET: 我试图使用这个片段,但仍然无法正常工作..
<f:metadata>
<f:viewParam name="cookieValue" value="#{bean.cookieValue}"/>
<f:event listener="#{bean.redirectPage}" type="preRenderView"/>
</f:metadata>
这是我的redirectpageMethod
public void redirectPage() throws IOException{
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
Cookie cookie = null;
Cookie[] cookieList = request.getCookies();
if(cookieList != null && cookieList.length > 0) {
for(int index = 0 ; index < cookieList.length ; index++) {
if(cookieList[index].getName().equalsIgnoreCase("AppCookie")) {
cookie = cookieList[index];
System.out.println("COOKIE FETCH");
break;
}
}
}
if(cookie.getValue().equals("0")) {
externalContext.redirect(externalContext.getRequestContextPath() + "/homepage.xhtml");
System.out.println("THIS COOKIE VALUE IS ZERO");
}
}