JSF 不显示第一行、第一列数据

发布于 2024-08-14 13:41:04 字数 6134 浏览 5 评论 0原文

我有一个非常简单的页面,显示预设数据。我在处理数据库数据时遇到了这个问题,所以我做了这个简单的例子,但问题仍然存在。当第一次导航到此页面进行会话时,第一行的第一列不显示任何数据。

如果我将范围更改为请求或会话并不重要,如果我使用 Firefox 或 IE 也没有什么区别。我所知道的是,如果我重新加载页面或导航离开并返回,它会突然显示我的数据 - 直到我关闭该浏览器的所有实例并加载一个新浏览器。然后,在我重新加载页面之前,它不会显示我的数据。

问题是为什么我心爱的第一本书的第一个标题直到我重新加载页面才显示(“操作系统概念”没有显示,但“Silberschatz”显示)?

下面是相关代码。

HomePage.java:

public class HomePage {

    private Book[] books;

    public HomePage() {
        books = new Book[3];
        books[0] = new Book("Operating System Concepts", "Silberschatz");
        books[1] = new Book("Learning Sql", "Beaulieu");
        books[2] = new Book("Effective Java", "Bloch");
    }

    public Book[] getBooks() {
        return books;
    }
    public void setBooks(Book[] books) {
        this.books = books;
    }
}

Book.java

public class Book {

    private String title;
    private String author;

    public Book() {}

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

home.jsp:

<html>
  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

  <link href="../css/styles.css" rel="stylesheet" type="text/css" />
  <f:loadBundle basename="omitted.for.anonymity.bundle.home" var="msgs"/>

  <f:view>
    <body>
      <h:form>
        <h:panelGrid columns="2" columnClasses="sideControlColumn,contentColumn">
          <f:facet name="header">
            <f:subview id="header">
              <jsp:include page="header.jsp" />
            </f:subview>
          </f:facet>

          <f:subview id="sideControl" >
            <jsp:include page="sideControl.jsp" />
          </f:subview>

          <h:dataTable value="#{homePage.books}"
                var="book"
                styleClass="homeTable"
                rowClasses="evenColumn,oddColumn"
                >

            <h:column>
              <f:facet name="header">
                <h:outputText value="#{msgs.columnHeader1}"/>
              </f:facet>

              <h:outputText value="#{book.title}" />
            </h:column>

            <h:column>
              <f:facet name="header">
                <h:outputText value="#{msgs.columnHeader2}"/>
              </f:facet>

              <h:outputText value="#{book.author}" />
            </h:column>

          </h:dataTable>

        </h:panelGrid>
      </h:form>
    </body>
     </f:view>
</html>

faces-config.xml:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

    <navigation-rule>
    <from-view-id>/pages/login.jsp</from-view-id>
        <navigation-case>
            <from-outcome>login</from-outcome>
            <to-view-id>/pages/home.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/home.jsp</from-view-id>
        <navigation-case>
            <from-outcome>searchForBooks</from-outcome>
            <to-view-id>/pages/searchCriteria.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/searchCriteria.jsp</from-view-id>
        <navigation-case>
            <from-outcome>searchForBooks</from-outcome>
            <to-view-id>/pages/searchCriteria.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/searchResults.jsp</from-view-id>
        <navigation-case>
            <from-outcome>searchForBooks</from-outcome>
            <to-view-id>/pages/searchCriteria.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/searchCriteria.jsp</from-view-id>
        <navigation-case>
            <from-outcome>search</from-outcome>
            <to-view-id>/pages/searchResults.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>


    <managed-bean>
        <managed-bean-name>book</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.Book</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>homePage</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.page.HomePage</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>searchCriteriaPage</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.page.SearchCriteriaPage</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>searchResultsPage</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.page.SearchResultsPage</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

</faces-config>

I have a very simple page that is displaying canned data. I experienced this problem with data from a DB so I made this simple example and still have the issue. When this page is navigated to for the first time for a session, the first column in the first row shows no data.

It doesn't matter if I change the scope to request or session, nor does it make a difference if I use firefox or IE. All I know is that if I reload the page or navigate away and come back it will all of a sudden show my data - until I close all instances of that browser and load up a new one. Then, again, it won't show my data until I reload the page.

The question is why does my beloved first title of my first book never show up until i reload the page ("Operating System Concepts" doesn't show, but "Silberschatz" does)?

Below is the relevant code.

HomePage.java:

public class HomePage {

    private Book[] books;

    public HomePage() {
        books = new Book[3];
        books[0] = new Book("Operating System Concepts", "Silberschatz");
        books[1] = new Book("Learning Sql", "Beaulieu");
        books[2] = new Book("Effective Java", "Bloch");
    }

    public Book[] getBooks() {
        return books;
    }
    public void setBooks(Book[] books) {
        this.books = books;
    }
}

Book.java

public class Book {

    private String title;
    private String author;

    public Book() {}

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

home.jsp:

<html>
  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

  <link href="../css/styles.css" rel="stylesheet" type="text/css" />
  <f:loadBundle basename="omitted.for.anonymity.bundle.home" var="msgs"/>

  <f:view>
    <body>
      <h:form>
        <h:panelGrid columns="2" columnClasses="sideControlColumn,contentColumn">
          <f:facet name="header">
            <f:subview id="header">
              <jsp:include page="header.jsp" />
            </f:subview>
          </f:facet>

          <f:subview id="sideControl" >
            <jsp:include page="sideControl.jsp" />
          </f:subview>

          <h:dataTable value="#{homePage.books}"
                var="book"
                styleClass="homeTable"
                rowClasses="evenColumn,oddColumn"
                >

            <h:column>
              <f:facet name="header">
                <h:outputText value="#{msgs.columnHeader1}"/>
              </f:facet>

              <h:outputText value="#{book.title}" />
            </h:column>

            <h:column>
              <f:facet name="header">
                <h:outputText value="#{msgs.columnHeader2}"/>
              </f:facet>

              <h:outputText value="#{book.author}" />
            </h:column>

          </h:dataTable>

        </h:panelGrid>
      </h:form>
    </body>
     </f:view>
</html>

faces-config.xml:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

    <navigation-rule>
    <from-view-id>/pages/login.jsp</from-view-id>
        <navigation-case>
            <from-outcome>login</from-outcome>
            <to-view-id>/pages/home.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/home.jsp</from-view-id>
        <navigation-case>
            <from-outcome>searchForBooks</from-outcome>
            <to-view-id>/pages/searchCriteria.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/searchCriteria.jsp</from-view-id>
        <navigation-case>
            <from-outcome>searchForBooks</from-outcome>
            <to-view-id>/pages/searchCriteria.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/searchResults.jsp</from-view-id>
        <navigation-case>
            <from-outcome>searchForBooks</from-outcome>
            <to-view-id>/pages/searchCriteria.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
    <from-view-id>/pages/searchCriteria.jsp</from-view-id>
        <navigation-case>
            <from-outcome>search</from-outcome>
            <to-view-id>/pages/searchResults.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>


    <managed-bean>
        <managed-bean-name>book</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.Book</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>homePage</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.page.HomePage</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>searchCriteriaPage</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.page.SearchCriteriaPage</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>searchResultsPage</managed-bean-name>
        <managed-bean-class>omitted.for.anonymity.beans.page.SearchResultsPage</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

</faces-config>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-08-21 13:41:04

设置 book 请求范围变量的表:

<h:dataTable value="#{homePage.books}"
    var="book"
    styleClass="homeTable"
    rowClasses="evenColumn,oddColumn">

设置 book 会话范围变量的托管 bean:

<managed-bean>
    <managed-bean-name>book</managed-bean-name>
    <managed-bean-class>omitted.for.anonymity.beans.Book</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

只是猜测,但我想知道表达式语言是否正在解析此处的会话 bean出于某种原因。它不应该 - ScopedAttributeELResolver< /a> “在页面、请求、会话和应用程序范围中搜索具有给定名称的属性并返回它,如果不存在具有当前名称的属性,则返回 null。”表达式语言实现中可能存在错误。尝试更改 var 属性的名称以避免冲突,或使用 #{requestScope.book.title} 将其显式解析为范围。

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

我从您的文档类型中注意到您正在使用 JSF 1.1。如果可以的话,我建议至少升级到 1.2(最好是 2.0)。

Table that sets a book request scope variable:

<h:dataTable value="#{homePage.books}"
    var="book"
    styleClass="homeTable"
    rowClasses="evenColumn,oddColumn">

Managed bean that sets a book session scope variable:

<managed-bean>
    <managed-bean-name>book</managed-bean-name>
    <managed-bean-class>omitted.for.anonymity.beans.Book</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Just guessing, but I wonder if the Expression Language is resolving the session bean here for some reason. It shouldn't - the ScopedAttributeELResolver "searches the page, request, session and application scopes for an attribute with the given name and returns it, or null if no attribute exists with the current name." But it isn't beyond possibility that there is a bug in the Expression Language implementation. Try changing the name of the var attribute to avoid collision or resolve it explicitly to the scope using #{requestScope.book.title}.

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

I notice from your doctype that you're using JSF 1.1. I recommend upgrading to at least 1.2 (preferably 2.0) if you can.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文