【问题标题】:error with redirect using listener JSF 2.0使用侦听器 JSF 2.0 重定向时出错
【发布时间】:2012-10-11 21:26:09
【问题描述】:

我有一个 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?

【问题讨论】:

  • getRealPath()?嗯?您为什么尝试重定向到本地磁盘文件系统路径而不是 URL?
  • 夜间工作很糟糕。你是对的

标签: jsf redirect jsf-2


【解决方案1】:

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

尝试制作:

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

【讨论】:

  • 它有帮助。但是当我被重定向到 list.xhtml 时,我发现我失去了价值 itemsList
  • 好吧,如果您的 bean 在会话范围内,则不应该发生这种情况,并且与此无关。将来当您有新问题时,请针对该问题提出一个新问题。这样您可以获得更多答案,并且问题更易于阅读。
猜你喜欢
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 2011-11-03
  • 2012-04-08
  • 2012-07-25
  • 2011-05-02
相关资源
最近更新 更多