我有一个非常简单的页面,显示固定数据。我使用来自数据库的数据遇到了这个问题所以我做了这个简单的例子,但仍然存在问题。当此页面第一次导航到会话时,第一行中的第一列不显示任何数据。
如果我将范围更改为请求或会话并不重要,如果我使用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>
答案 0 :(得分:2)
设置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 “搜索具有给定名称的属性的页面,请求,会话和应用程序范围并返回它,如果当前名称不存在属性,则返回null。” em>但是,表达式语言实现中存在错误并非超出可能性。尝试更改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">
我从你的doctype中注意到你正在使用JSF 1.1。如果可以,我建议升级到至少1.2(最好是2.0)。