Redirect before/during page load

Splash Forums PrettyFaces Users Redirect before/during page load

This topic contains 5 replies, has 3 voices, and was last updated by  DRY2 9 years, 11 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #18172

    DRY2
    Participant

    Hello,

    I’m trying to redirect using PrettyFaces during or before page load. Use case: when a user goes to index page check if a browser cookie exists, if cookie exist, redirect to second page otherwise stay on index. Currently when I try to redirect on page load I get a Response already committed error. Below is our code for the redirect. Does anyone have a method to redirect during or before page load? Is this even possible?

    // PrettyFaces redirect

    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

    HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();

    PrettyContext context = PrettyContext.getCurrentInstance(request);

    PrettyURLBuilder builder = new PrettyURLBuilder();

    // set PrettyFaces url-mapping id

    UrlMapping mapping = context.getConfig().getMappingById(mappingId);

    String targetURL = context.getContextPath() + builder.build(mapping, true, params) +

    “?_adf.ctrl-state=” + request.getParameterValues(“_adf.ctrl-state”)[0];

    targetURL = response.encodeRedirectURL(targetURL);

    response.sendRedirect(targetURL);

    FacesContext.getCurrentInstance().responseComplete();

    Also, I found this link, but unsure if a filter will work for this use case

    http://ocpsoft.org/support/topic/redirect-to-the-saved-first-requested-url-after-loggin

    Thanks for your time,

    -DY

    #22168

    Yes this is possible and should work without problems.

    Where did you add this code? In a page load action?

    Could you please post the full stacktrace of the “already committed” exception?

    #22169

    yep. this should work. you can also use OCPsoft rewrite for this type of manipulation. http://ocpsoft.org/rewrite/

    #22170

    DRY2
    Participant

    Thx for response. Bellow is the info you asked for.

    // page load

    public void loadIndex(PhaseEvent phaseEvent) {

    prettyRedirectCity(); }

    // redirect city method

    public void prettyRedirectCity() {

    if(!getCookie(“defaultCity”).equalsIgnoreCase(“null”)){

    city = getCookie(“defaultCity”);

    updateStateParam();

    this.prettyFacesRedirect(“city”, new Object[] { state, city });

    }

    }

    The prettyFacesRedirect method is the code from my original post.

    //stack trace

    java.lang.IllegalStateException: Response already committed

    at weblogic.servlet.internal.ServletResponseImpl.objectIfCommitted(ServletResponseImpl.java:1608)

    at weblogic.servlet.internal.ServletResponseImpl.sendRedirect(ServletResponseImpl.java:834)

    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:136)

    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:136)

    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:136)

    at siteUtils.siteTasks.prettyFacesRedirect(siteTasks.java:4348)

    at siteUtils.siteTasks.prettyRedirectCity(siteTasks.java:4380)

    at siteUtils.siteTasks.loadIndex(siteTasks.java:270)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:597)

    at com.sun.el.parser.AstValue.invoke(Unknown Source)

    at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)

    at javax.faces.component.UIViewRoot.notifyPhaseListeners(UIViewRoot.java:608)

    at javax.faces.component.UIViewRoot.notifyBefore(UIViewRoot.java:510)

    at javax.faces.component.UIViewRoot.encodeBegin(UIViewRoot.java:564)

    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:928)

    at com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:266)

    at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:197)

    at com.ocpsoft.pretty.faces.application.PrettyViewHandler.renderView(PrettyViewHandler.java:87)

    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:189)

    at org.apache.myfaces.trinidadinternal.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:193)

    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._renderResponse(LifecycleImpl.java:800)

    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:294)

    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:214)

    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)

    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)

    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)

    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)

    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:205)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:106)

    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:446)

    at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)

    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:446)

    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:271)

    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:177)

    at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at oracle.adf.library.webapp.LibraryFilter.doFilter(LibraryFilter.java:175)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:126)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:524)

    at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:253)

    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:111)

    at java.security.AccessController.doPrivileged(Native Method)

    at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:313)

    at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:413)

    at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:94)

    at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:161)

    at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:71)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:136)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)

    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)

    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)

    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)

    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)

    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)

    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)

    at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

    FYI: at siteUtils.siteTasks.prettyFacesRedirect(siteTasks.java:4348) is pointing to response.sendRedirect(targetURL);

    #22171

    You are executing your method within RENDER_RESPONSE:

    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:928)
    at com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:266)
    at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:197)

    Therefore it is reasonable you get this exception as parts of the page are already rendered. You should do the redirect earlier. Like before RESTORE_VIEW. You could also do this from within a PrettyFaces page action which is executed exactly at this point in time.

    Another option would be to use Rewrite as Lincoln mentioned. It’s very easy to implement something like this in Rewrite.

    #22172

    DRY2
    Participant

    Thank you Christian. Using PrettyFaces page action solved my problem. Below was the code used.

    // index page mapping inside pretty config

    <url-mapping id=”home”>

    <pattern value=”/” />

    <view-id value=”/faces/pages/index.jspx” />

    <action>#{siteTasks.prettyRedirectCity}</action>

    </url-mapping>

    // redirect method

    public String prettyRedirectCity() {

    if(!getCookie(“defaultCity”).equalsIgnoreCase(“null”)){

    city = getCookie(“defaultCity”);

    updateStateParam();

    return “pretty:city”;

    }else{

    return null;

    }

    }

Viewing 6 posts - 1 through 6 (of 6 total)

The forum ‘PrettyFaces Users’ is closed to new topics and replies.

Comments are closed.