JSF DataTable添加行

时间:2015-08-23 11:48:45

标签: jsf datatable

我正在尝试在JSF中执行数据表示例,我有以下代码:

package com.boddooo.game;

public class person {

    private int id;
    private String name;
    private String city;
    private int age;

    public person(int id, String name, String city, int age) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

和bean类

package com.boddooo.game;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "personsData")
@SessionScoped
public class personsData implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private String city;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private static final ArrayList<person> persons = new ArrayList<person>(Arrays.asList(
            new person(1, "mohamed", "zag", 36), new person(2, "camel", "cairo", 45)));


    public ArrayList<person> getPersons() {
        return persons;
    }

    public String addPerson() {
        person p = new person(this.id, this.name, this.city, this.age);
        persons.add(p);
        return null;
    }

    public String delPerson(person p) {
        persons.remove(p);
        return null;
    }

}

xhtml文件是

<?xml version="1.0" encoding="windows-1256" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:h="http://java.sun.com/jsf/html"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Boddooo JSF Tutorial!</title>
    <h:outputStylesheet name="css/styles.css"/>
</h:head>
<h:body>
    <h:form>
        <h:dataTable value="#{personsData.persons}" var="persons" styleClass="personTable"
                     headerClass="personTableHeader"
                     rowClasses="personTableOddRow,personTableEvenRow">>
            <h:column>
                <f:facet name="header">ID</f:facet>#{persons.id}
            </h:column>
            <h:column>
                <f:facet name="header">Name</f:facet>#{persons.name}
            </h:column>
            <h:column>
                <f:facet name="header">City</f:facet>#{persons.city}
            </h:column>
            <h:column>
                <f:facet name="header">Age</f:facet>#{persons.age}
            </h:column>
            <h:column>
                <f:facet name="header">Action</f:facet>
                <h:commandLink value="Delete" action="#{personsData.delPerson(persons)}"></h:commandLink>
            </h:column>
        </h:dataTable>
    </h:form>
    <br></br>
    <table>
        <tr>
            <td>ID</td>
            <td><h:inputText size="10" value="#{personsData.id}"></h:inputText></td>
        </tr>
        <tr>
            <td>Name</td>
            <td><h:inputText size="30" value="#{personsData.name}"></h:inputText></td>
        </tr>
        <tr>
            <td>City</td>
            <td><h:inputText size="30" value="#{personsData.city}"></h:inputText></td>
        </tr>
        <tr>
            <td>Age</td>
            <td><h:inputText size="10" value="#{personsData.age}"></h:inputText></td>
        </tr>
        <tr>
            <td colspan="2">
                <h:commandButton action="#{personsData.addPerson}" value="Add"> </h:commandButton>
            </td>
        </tr>
    </table>
</h:body>
</html>

问题出在addPerson方法中它什么也不做,其他一切都没问题。 我正在使用tomEE

0 个答案:

没有答案