gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:14:52 27 4
gpt4 key购买 nike

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

如果我将范围更改为请求或 session 并不重要,如果我使用 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>

最佳答案

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

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

设置 book session 范围变量的托管 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>

只是猜测,但我想知道表达式语言是否由于某种原因正在解析此处的 session bean。它不应该 - ScopedAttributeELResolver “在页面、请求、 session 和应用程序范围中搜索具有给定名称的属性并返回它,如果不存在具有当前名称的属性,则返回 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)。

关于java - JSF 不显示第一行、第一列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1917923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com