Problem with redirecting in JSF 2

5

In a JSF project the pages are organized like this:

  • admin / pages
  • admin / main
  • admin / template.

My web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>ararazul</display-name>

    <welcome-file-list>
        <welcome-file>/admin/index.xhtml</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

</web-app>

I created a class to encapsulate the redirection according to the use case.

package br.com.ararazul.util;

import java.io.IOException;

import javax.faces.context.FacesContext;

/**
 *
 * Classe responsável por abstrair as navegações do Sistema
 * 
 * @author Hugo Sousa
 * 
 * */
public class NavigationUtil {

    public static final String PAGESFOLDER = "admin";
    public static final String INITIALPAGE = "index";

    /**
     * 
     * Método responsável por forçar o redirecionamento para uma determinada
     * página no contexto do caso de uso em questão
     * 
     * @author Hugo Sousa
     * @param casoDeUso
     * @param pagina
     * @throws IOException 
     * 
     * */
    public static void redirecionar(String casoDeUso, String pagina) {

        FacesContext facesContext = FacesContext.getCurrentInstance();

        String contexto = facesContext.getExternalContext().getContextName();
        try {
            if(pagina.equals(INITIALPAGE)) {
                facesContext.getExternalContext().redirect("/" + contexto);
            } else {
                facesContext.getExternalContext().redirect("/" + contexto + "/" + PAGESFOLDER + "/" + casoDeUso + "/" + pagina + ".xhtml");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

But there is a loop in the redirect every time you access the application. I've just used a method call from a controller to call the index in the system menu: #{controller.index()} and when I throw that call the page loads normally.

    
asked by anonymous 21.11.2015 / 14:01

1 answer

0

Try removing the call {controller.index ()} and add this code to your web.xml.

<welcome-file-list>
   <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
    
22.11.2015 / 13:56