JSF 导航重定向到上一页 [英] JSF navigation redirect to previous page

查看:22
本文介绍了JSF 导航重定向到上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦用户成功登录系统,系统会将用户重定向到首页.现在我的问题是,如果用户在未登录系统的情况下点击查看帐户页面,系统会将用户重定向到登录页面.如果用户现在登录系统,系统会将用户重定向到主页,在这种情况下,任何方法都可以将用户重定向到上一个页面,即查看帐户页面而不是主页?

Once the user successful login to the system, the system will redirect the user to the homepage. Now my problem is, if the user clicks on the view account page without login to the system the system will redirect user to the login page. if user login to the system now the system will redirect the user to the homepage, in this case any method can redirect user to the previous page that is view account page instead of homepage?

我尝试使用会话

String url = (String)session.getAttribute("url");
if(url != null)
    response.sendRedirect(url);
else
    response.sendRedirect("homepage.faces");

如果用户登录成功,我将这段代码放在 public void doBtnAction(){} 下,然后重定向到 url.但是我收到了这个错误

i put this code under public void doBtnAction(){} if the user login successful then redirect to the url. But i got this error

java.lang.IllegalStateException: Cannot forward after response has been committed
    com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
    com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:130)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
    com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)

推荐答案

这个异常是因为你调用了 response.sendRedirect() 并且没有阻止 JSF 渲染正常的响应.您需要通过添加通知JSF它不需要处理正常响应

This exception is caused because you called response.sendRedirect() and didn't block JSF from rendering the normal response. You need to inform JSF that it doesn't need to handle normal response by adding

FacesContext.getCurrentInstance().responseComplete(); 

到动作方法.

或者,更好的是,不要从 JSF 的底层获取 HttpServletResponse,而是这样做:

Or, even better, just don't get the HttpServletResponse from under the JSF's hoods, but instead do:

FacesContext.getCurrentInstance().getExternalContext().redirect(url);

这将自动调用 responseComplete() 并且它还可以使您的代码保持干净,避免不必要的 Servlet API 内容.另请参阅 ExternalContext API.

This will automatically invoke responseComplete() and it also keeps your code clean from unnecessary Servlet API stuff. Also see the ExternalContext API.

这篇关于JSF 导航重定向到上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆