0

我有一个 index.xhtml 页面

<!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>

在具有范围会话的 bean 类中,此方法

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);     
    }

我认为我应该重定向到下一页,但我有“浏览器无法显示页面”

和 list.xhtml (如果我将此页面作为欢迎页面我没有错误,这意味着该错误与重定向有关)

<!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>

在 consol 中我没有任何错误。

在 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>

这个问题和我在重定向后丢失值 itemsList 的原因可能是什么?

4

1 回答 1

1

该方法redirect()必须接收有效的 URL,而您传递给它的内容无效,主要是因为您使用的是反斜杠\\而不是普通的斜杠/

尝试制作:

String realPath = rootPath + "pages/template/body/list.xhtml";
于 2012-10-11T22:17:49.963 回答