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

查看:118
本文介绍了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呈现正常响应.您需要通过添加

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.另请参见

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天全站免登陆