ManagedBean with ArrayList,如何添加元素?

时间:2014-10-03 14:39:07

标签: jsf arraylist managed-bean

我正在尝试使用显示partyguest列表的SIMPLE webapp,并允许我添加新的访客。我想将客户存储在ArrayList中。我不知道在哪里以及如何调用party.addGuest()方法。

的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">
    <h:head>
        <title>Big Party</title>
    </h:head>
    <h:body>
        <h2>Add new guest to Big Party: </h2>
        <h:form>
            <h:inputText id="guestName" value="#{guest.name}"/>
            <h:commandButton value="Add guest" action="guests" />
        </h:form>
        <h:link value="GuestList" outcome="guests" />
    </h:body>
</html>

guests.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:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Super Party</title>
    </h:head>
    <h:body>
        <h2>New guest:</h2>
        <h:outputLabel value="#{guest.name}" />

        <h2>Guests:</h2>
        <ul>
            <ui:repeat value="#{party.guests}" var="curr">
                <li>#{curr}</li>
            </ui:repeat>
        </ul>

        <h2>Guests count:</h2>
        <h:outputLabel value="#{party.cnt}"/>
    </h:body>
</html>

Party.java

package managedBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean(name = "party")
@ApplicationScoped
public class Party implements Serializable {

    private List<String> guests;
    private int cnt;

    public Party() {
    }

    @PostConstruct
    public void init() {
        guests = new ArrayList<>();
        guests.add("Guest A");
        guests.add("Guest B");
    }

    public List<String> getGuests() {
        return guests;
    }

    public void addGuest(String guest) {
        guests.add(guest);
    }

    public int getCnt() {
        cnt = guests.size();
        return cnt;
    }
}

Guest.java

package managedBeans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "guest")
@RequestScoped
public class Guest implements Serializable{

    private String name;

    public Guest() {
    }

    public String getName() {
        return name;
    }

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

1 个答案:

答案 0 :(得分:0)

如果你使用的是JSF 2,你可以在index.xhtml中使用这样的东西(但它可以很容易地转换为早期的JSF版本):

...
    <h:form>
        <h:inputText id="guestName" value="#{party.newGuest}"/>
        <h:commandButton value="Add guest" action="#{party.addGuest()}" />
    </h:form>
...

而且,在Party.java

private String newGuest;
....
public String getNewGuest() {
    return this.newGuest;
}

public void setNewGuest(String guest) {
    this.newGuest = guest;
}
....
public void addGuest() {
    guests.add(newGuest);
    newGuest = null;
}

此用例中不需要Guest.java。但是,可以以更好的方式完成。