我有一个非常简单的页面显示预设数据。我在使用数据库中的数据时遇到了这个问题,所以我做了这个简单的例子,但仍然有问题。当会话第一次导航到此页面时,第一行中的第一列不显示任何数据。
我是否将范围更改为请求或会话都没有关系,如果我使用 firefox 或 IE 也没有什么不同。我所知道的是,如果我重新加载页面或导航离开并返回,它会突然显示我的数据 - 直到我关闭该浏览器的所有实例并加载一个新实例。然后,在我重新加载页面之前,它不会显示我的数据。
问题是为什么在我重新加载页面之前,我心爱的第一本书的第一个标题从未出现(“操作系统概念”没有显示,但“Silberschatz”有)?
下面是相关代码。
主页.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;
}
}
图书.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;
}
}
主页.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>
面孔-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>