jsf - 为什么我的 Java Server Faces HTML 数据表没有填充?
问题描述
我正在按照书中的说明创建 JSF 网页。创建数组列表的 init() 方法。此列表填充数据表,但网页上没有显示任何内容。我在 init 方法中放置了一条日志消息,但没有显示任何消息。
编辑:Java 1.8.0_222、Payara 5.193.1、NetBeans 8.2
javaee-web-api-8.0、javax.faces-2.2.7、javaee-endorsed-api-7.0
索引.html
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [<!ENTITY copy "©">]>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Books</title>
<h:outputStylesheet library="css" name="books.css"/>
</h:head>
<h:body>
<div id="wrapper">
<header>
<h1>Michael's book list</h1>
</header>
<main>
<h1>Edit categories</h1>
<h:form>
<h:dataTable value="#{categoryEditor.categories}"
var="cat">
<h:column>
<h:commandLink
action="#{categoryEditor.deleteCategory(cat)}">
<h:graphicImage alt="delete"
name="Delete.png"
library="icon/small"
title="delete"/>
</h:commandLink>
</h:column>
<h:column>
<h:inputText value="#{cat.name}"/>
</h:column>
</h:dataTable>
<h:commandLink styleClass="button"
value="Add category"
action="#{categoryEditor.addCategory}"/>
<h:commandButton styleClass="button"
value="Save"
action="#{categoryEditor.save}"/>
</h:form>
</main>
<nav>
This is the Navigation
</nav>
<footer>
© Michael Muller <!-- © refers to the copyright sign
HTML5 doesn't know such entities, so doctype must be enhanced-->
<h:outputLink value="http://blog.mueller-bruehl.de">
Michael's Blog
</h:outputLink>
<h:link value="About" outcome="index.xhtml"/>
</footer>
</div>
</h:body>
</html>
类别编辑器.java
package us.potato.books;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class CategoryEditor implements Serializable{
private static final long serialVersionUID = 1L;
private static final Logger _logger = Logger.getLogger("CategoryEditor");
@PostConstruct
private void init() {
_categories = new ArrayList<>();
_categories.add(new Category(){{setId(1); setName("Java");}});
_categories.add(new Category(){{setId(2); setName("Web");}});
_logger.log(Level.INFO, "Categories initialized");
}
private List<Category> _categories;
public List<Category> getCategories() {
return _categories;
}
public void setCategories(List<Category> categories) {
_categories = categories;
}
public String addCategory(){
_categories.add(new Category());
return "";
}
public String deleteCategory(Category category){
_categories.remove(category);
return "";
}
public String save(){
String categories = "";
for (Category category : _categories){
if(!category.getName().isEmpty()){
if (categories.length() > 0){
categories += ", ";
}
categories += category;
}
}
_logger.log(Level.INFO, "Save categories: {0}", categories);
return "";
}
}
类别.java
package us.potato.books;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class CategoryEditor implements Serializable{
private static final long serialVersionUID = 1L;
private static final Logger _logger = Logger.getLogger("CategoryEditor");
@PostConstruct
private void init() {
_categories = new ArrayList<>();
_categories.add(new Category(){{setId(1); setName("Java");}});
_categories.add(new Category(){{setId(2); setName("Web");}});
_logger.log(Level.INFO, "Categories initialized");
}
private List<Category> _categories;
public List<Category> getCategories() {
return _categories;
}
public void setCategories(List<Category> categories) {
_categories = categories;
}
public String addCategory(){
_categories.add(new Category());
return "";
}
public String deleteCategory(Category category){
_categories.remove(category);
return "";
}
public String save(){
String categories = "";
for (Category category : _categories){
if(!category.getName().isEmpty()){
if (categories.length() > 0){
categories += ", ";
}
categories += category;
}
}
_logger.log(Level.INFO, "Save categories: {0}", categories);
return "";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>us.potato</groupId>
<artifactId>Books</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Books</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
web.xml
<web-app version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>\
nb-configuration.xml
<?xml version="1.0" encoding="UTF-8"?>
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>1.7-web</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
<org-netbeans-modules-projectapi.jsf_2e_language>Facelets</org-netbeans-modules-projectapi.jsf_2e_language>
</properties>
</project-shared-configuration>
解决方案
推荐阅读
- c# - 如何在控制器中重定向未经授权的用户?
- c# - .net Core 注册给定程序集的所有服务
- laravel - 带条件的 Lavarel 查询
- java - Gradle 构建失败 - 3.6.1
- javascript - 我遇到了一个问题,我使用 chart.js 的图表直到我在标签按钮上点击 3 次才出现
- c++ - 尚未声明奇怪的“类名”
- c# - 从控制器生成角度路由的 URL
- javascript - 解析 fetch() JSON 响应输出 Nodejs 与解析 JSON 变量
- django - 如何在 Django 基本模板中呈现数据库查询集?
- java - application.conf 文件中的 AWS Api 凭证 - 足够安全吗?