Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

In some instances you need to do a redirect to a URL outside your application, for example when a given page is widely linked by external sites and is moved to another application, or on SEO situations that require a 301.

package com.boulevardr.ui;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

@Name("permanentRedirector") public class PermanentRedirector
{
        @In FacesContext facesContext;
        
        public void toUrl(String location)
        {
                HttpServletResponse res = (HttpServletResponse)facesContext.getExternalContext().getResponse();
                res.setContentType("text/html");
                res.setHeader("Location", location);
                res.setStatus(301);
                facesContext.responseComplete();
        }
}

on pages.xml:

<page view-id="/page.seam" action="#{permanentRedirector.toUrl('http://site.to.redirect')}" />

It was suggested that this kind of redirect should be done in a Servlet. However, I prefer to keep all flow-related rules on a single location (i. e.: pages.xml), and that was my main motivation behind this solution.