我对 Java 和 JSF 有点陌生,但这里有:
在eclipse中,新建一个JSF项目:
我已经配置了 hibernate/jpa 等。Eclipse 可以与 Postgres 数据库通信并从实体创建表。
我已经声明了一个名为 BlogPost 的实体和一个 BlogPostController(BlogPost 实体的托管逻辑)。
index.xhtml 文件扩展了一个名为 base.xhtml 的模板,该模板定义了几个区域。我正在尝试使用 ui:repeat 和 h:dataTable 从 index.xhtml 中引用托管 bean 方法,但即使数据库中有博客文章条目,也没有出现任何内容。
我也没有在日志中收到任何错误。有人请帮忙,如果缺少某些东西,我很乐意提供更多信息。
package org.nps2.blog.beans;
import java.util.List;
import org.jboss.logging.Logger;
import org.nps2.blog.controllers.BlogPostController;
import org.nps2.blog.models.*;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
@ManagedBean
public class Homepage {
@SuppressWarnings("unused")
private List<BlogPost> blogposts;
private List<User> users;
private static final Logger log = Logger.getLogger(Homepage.class.getName());
public void setUsers(List<User> users) {
this.users = users;
}
@Inject
private BlogPostController c;
public List<User> getUsers() {
log.info("HELLO THERE");
return (users);
}
public List<BlogPost> getBlogposts() {
log.info("HELLO THERE");
return (c.getAll());
}
public void setBlogposts(List<BlogPost> blogposts) {
this.blogposts = blogposts;
}
}
索引.xhtml
<h:dataTable value="#{homepage.blogpost}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Customer ID!
</f:facet>
#{c.id}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.first_name}
</h:column>
<h:column>
<f:facet name="header">
Last name
</f:facet>
#{c.last_name}
</h:column>
<h:column>
<f:facet name="header">
Username
</f:facet>
#{c.username}
</h:column>
</h:dataTable>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Blog</display-name>
<welcome-file-list>
<welcome-file>index.jsp</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>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
</web-app>