【问题标题】:How can I get my ArrayList in my JSF to dynamically print?如何让我的 JSF 中的 ArrayList 动态打印?
【发布时间】:2023-04-03 08:36:01
【问题描述】:

当我使用列出的项目运行 JSF 时,我会出现第一部分,我可以在其中成功输入详细信息,但它们不会像我希望的那样显示在屏幕上!各位大神帮忙看看:

这是我的 index.xhtml 代码:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>JSF Tester</title>
    </h:head>
    <h:body>
        <h:form>
            First name: <h:inputText value="#{PersonController.current.firstName}"/>
            Last name: <h:inputText value="#{PersonController.current.lastName}"/>
            Birth date: (mm-dd-yyyy) <h:inputText value="#{PersonController.current.date}">
                <f:convertDateTime dateStyle="full" type="date" timeZone="CST" pattern="MM-dd-yyyy"/>
            </h:inputText>
            <h:commandButton value="Submit Person" action="#{PersonController.savePerson()}" />

            <h:dataTable var="persons" value="#{PersonController.current}">
                <h:column>
                    <f:facet name="header">First Name</f:facet>
                        #{PersonController.current.firstName}
                </h:column>
                <h:column>
                    <f:facet name="header">Last Name</f:facet>               
                        #{PersonController.current.lastName}
                </h:column>
                <h:column>
                    <f:facet name="header">Date</f:facet>
                        #{PersonController.current.date}
                </h:column>
            </h:dataTable>

        </h:form>
    </h:body>
</html>

这是我的包含 ArrayList 的类:

/**
 $ Id: PersonController.java v1.0, 7/16/2014
 $ Name: $
 */
package org.usd.csci.person.jsf;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author 
 */
@ManagedBean(name="PersonController")
@SessionScoped
public class PersonController implements Serializable {

    Person current = new Person();
    List<Person> persons = new ArrayList<Person>();

    /**
     * Creates a new instance of PersonController
     */
    public PersonController() {
    }

    public Person getCurrent() {
        return current;
    }

    public List<Person> getPersons() {
        return persons;
    }

    public void savePerson() {
        persons.add(current);
        current = new Person();
    }
}

【问题讨论】:

  • 你好吗want them to

标签: jsf arraylist datatable


【解决方案1】:

您以错误的方式设置数据表。应该如下所示。

value 参数应寻址ArrayList。在你的情况下PersonController.persons。

var 参数用于寻址persons 列表中的Person 对象。你可以随意命名。

访问Person 对象的属性您将使用此var 参数。例如,如果您将var 设置为 var = "variableName",您可以使用variableName.firstName variableName.lastName 等访问Person 对象的属性。

<h:dataTable var="person" value="#{PersonController.persons}">
                <h:column>
                    <f:facet name="header">First Name</f:facet>
                        #{person.firstName}
                </h:column>
                <h:column>
                    <f:facet name="header">Last Name</f:facet>               
                        #{person.lastName}
                </h:column>
                <h:column>
                    <f:facet name="header">Date</f:facet>
                        #{person.date}
                </h:column>
            </h:dataTable>

【讨论】:

  • 这绝对是完美的。有没有办法可以将日期打印为 mm-dd-yyyy?我有代码集
猜你喜欢
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
相关资源
最近更新 更多