我假设你有一个Address
地址类。和一个AddressBean
用一个List
来保存地址。
代码可能看起来像这样(对于我能想到的最基本的场景):
<h:form id="addressForm">
<h:commandButton value="Add Address" action="#{addressBean.addAddress()}" immediate="true" execute="@this">
<f:ajax render="addressForm" />
</h:commandButton>
<c:forEach items="#{addressBean.addressList}" var="address">
<h:inputText value="#{address.street}" /><br />
</c:forEach>
<h:commandButton value="Save" action="#{addressBean.persistAddresses}" />
</h:form>
@ManagedBean
@ViewScoped
public class AddressBean {
private List<Address> addressList = new ArrayList<Address>(); // getter+setter
public void addAddress() {
addressList.add(new Address());
}
public void persistAddresses() {
// store the addressList filled with addresses
}
}
public class Address {
private String address; // getter+setter
}
<c:forEach>
取自 JSTL。它可能适用于<ui:repeat>
,但取决于您的实际情况,它可能不会。