For some reason the XHTML page does not list the #{nomesMB.nomes}
items. Codes:
XHTML :
<!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>Lista de nomes</title>
</h:head>
<h:body>
<h:form>
Nome: <h:inputText value="#{nomesMB.nome}" binding="#{nomesMB.inputNome}"/>
<br />
<h:commandButton value="Adicionar"
action="#{nomesMB.adicionar()}" binding="#{nomesMB.botaoAdicionar}"/>
<br />
<ol>
<ui:repeat var="nome" value="#{nomesMB.nomes}">
<li>#{nome}</li>
</ui:repeat>
</ol>
</h:form>
</h:body>
</html>
Java :
package br.com.marciowillian.financeiro.managedbean;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlInputText;
import javax.faces.view.ViewScoped;
@ManagedBean(name="nomesMB")
@ViewScoped
public class NomesBean {
private String nome;
private List<String> nomes = new ArrayList<>();
private HtmlInputText inputNome;
private HtmlCommandButton botaoAdicionar;
public void adicionar() {
this.nomes.add(nome);
//desativa campo botao quando mais de 3 nomes forem adicionados
if(this.nomes.size() > 3) {
this.inputNome.setDisabled(true);
this.botaoAdicionar.setDisabled(true);
this.botaoAdicionar.setValue("Muitos nomes adicionados...");
}
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public List<String> getNomes() {
return nomes;
}
public HtmlInputText getInputNome() {
return inputNome;
}
public void setInputNome(HtmlInputText inputNome) {
this.inputNome = inputNome;
}
public HtmlCommandButton getBotaoAdicionar() {
return botaoAdicionar;
}
public void setBotaoAdicionar(HtmlCommandButton botaoAdicionar) {
this.botaoAdicionar = botaoAdicionar;
}
}