Forums de discussion

Enable Redirect during the ResourceResponse Phase

PortletFaces Community Member, modifié il y a 12 années.

Enable Redirect during the ResourceResponse Phase

Regular Member Publications: 199 Date d'inscription: 03/04/12 Publications récentes
As mentioned in the following Jira Issue: http://issues.liferay.com/browse/FACES-235 it would be very usefull to have the possibility to use a redirect
 eg:  FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.google.com")

even though it is not an done within a regular POST action.

How about chaning the redirect method in org.portletfaces.bridge.container.liferay.PortletContainerLiferayImpl to:


 @Override
    public void redirect(String url) throws IOException {
        PortletResponse portletResponse = getPortletResponse();
        if (portletResponse instanceof ActionResponse) {
            LiferayPortletResponse liferayActionResponse = new LiferayPortletResponse(portletResponse);
            liferayActionResponse.sendRedirect(url);
        } else if (portletResponse instanceof ResourceResponse) {
            final FacesContext ctx = FacesContext.getCurrentInstance();
            if (ctx.getPartialViewContext().isPartialRequest()) {
                ResourceResponse portletResourceResponse = (ResourceResponse) portletResponse;
                PartialResponseWriter pwriter;
                ResponseWriter writer = ctx.getResponseWriter();
                if (writer instanceof PartialResponseWriter) {
                    pwriter = (PartialResponseWriter) writer;
                } else {
                    pwriter = ctx.getPartialViewContext().getPartialResponseWriter();
                }
                portletResourceResponse.setContentType("text/xml");
                portletResourceResponse.setCharacterEncoding("UTF-8");
                // addResponseHeader("Cache-Control", "no-cache");
                pwriter.startDocument();
                pwriter.redirect(url);
                pwriter.endDocument();
                ctx.responseComplete();
            } else {
                throw new UnsupportedEncodingException(
                        "Can only redirect during RESOURCE_PHASE if a Partial-(JSF AJAX)-Request  has been triggered");
            }
        } else {
            throw new UnsupportedEncodingException("Can not redirect during the current phase: "
                    + portletResponse.getClass().getSimpleName());
        }
    }


Thus you'll be able to use both mechanism POST and AJAX Partial Request:

Example XHTML

		<h:form id="myForm1">
		This is a test to see how redirect is done within a ajax partial requect
			<h:commandbutton id="btn1" action="#{controllerBean.redirectAction}" value="test redirect ajax" />
		</h:form>

		<h:form id="myForm2">
		This is a test to see how redirect is done within a post 
			<ice:commandbutton id="btn1" action="#{controllerBean.redirectAction}" value="test redirect post">
				<f:ajax disabled="true" />
			</ice:commandbutton>
		</h:form>


Example Java Bean Code

public class ControllerBean {

    public void redirectAction() {
        System.out.println("Action Triggered");
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.google.com");
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
}
Narendra Verma, modifié il y a 12 années.

RE: Enable Redirect during the ResourceResponse Phase

New Member Envoyer: 1 Date d'inscription: 06/08/11 Publications récentes
This article really helped me to resolve the redirection issue.

Thanks a lot for providing this solution. emoticon
thumbnail
Neil Griffin, modifié il y a 12 années.

RE: Enable Redirect during the ResourceResponse Phase

Liferay Legend Publications: 2655 Date d'inscription: 27/07/05 Publications récentes
@Patrick: Thank you for the patch for the RESOURCE_PHASE/ResourceRequest/Ajax case. emoticon

I applied your patch as part of FACES-258 and it will appear in version 2.0.1 of the bridge.