Ray
Ray

Reputation: 1828

error with redirect using listener JSF 2.0

I have a index.xhtml page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<f:view>
    <ui:insert name="metadata" />
    <f:event type="preRenderView" listener="#{item.show}" />
    <h:body></h:body>
</f:view>
</html>

And in bean class with scope session this method

public void show() throws IOException, DAOException {

        ExternalContext externalContext = FacesContext.getCurrentInstance()
                .getExternalContext();

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            itemsList = itemsBo.showItems(); 

        String rootPath = externalContext.getRealPath("/");
        String realPath = rootPath + "pages\\template\\body\\list.xhtml";
        externalContext.redirect(realPath);     
    }

i think that I should redirect to next page but I have "browser can't show page"

and list.xhtml (if I do this page as welcome-page I haven't error, it means that error connected with redirect)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
    <ui:composition template="/pages/layouts/mainLayout.xhtml">
        <ui:define name="content">
            <h:form><h:dataTable value="#{item.itemsList}" var="itemVar"
                styleClass="order-table" headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row">

                <h:column>              
                #{itemVar.content}
        </h:column>
            </h:dataTable></h:form></ui:define></ui:composition>
</h:body>
</html>

in consol i didn't have any error.

in web.xml

<welcome-file-list>
        <welcome-file>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>

What can be the reason this problem and I lose value itemsList after redirect?

Upvotes: 0

Views: 517

Answers (1)

Elias Dorneles
Elias Dorneles

Reputation: 23796

The method redirect() have to receive a valid URL, and what you are passing to it is not valid, mainly because you are using backslashes \\ instead of normal slashes /.

Try making:

String realPath = rootPath + "pages/template/body/list.xhtml";

Upvotes: 1

Related Questions