Problem with JSF 2 redirection

Asked

Viewed 468 times

5

In a JSF project the pages are organized like this:

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

My web.xml is 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 redirect according to 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();
        }
    }

}

Except there’s a loop in the redirect every time I access the app. I just used a call to a controller method to call the index in the system menu: #{controller.index()} and when I take that call the page loads normally.

  • use "? faces-redirect=true" at the end of the url.

1 answer

0

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

<welcome-file-list>
   <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
  • I’ve already done this on web.xml. And that entry is for when I’m on another page and call the home page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.