How to insert inpuText data into a list using JSF?

Asked

Viewed 1,269 times

4

I have the following problem:

I need a form to submit questions of evidence. Therefore, the form should contain a field for the question (question statement), and several fields for the alternatives.

With this, I wanted the input values of the alternatives to go straight to a list of instantiated alternatives in the respective bean.

So we have the Option class:

public class Opcao {

    private int idOpcao;
    private String nomeOpcao;

    public int getIdOpcao() {
        return idOpcao;
    }
    public void setIdOpcao(int idOpcao) {
        this.idOpcao = idOpcao;
    }
    public String getNomeOpcao() {
        return nomeOpcao;
    }
    public void setNomeOpcao(String nomeOpcao) {
        this.nomeOpcao = nomeOpcao;
    }

}

And the bean containing the list of options:

@ManagedBean
@ViewScoped
public class QuestaoBean {

    private List<Opcao> opcoes;

    public List<Opcao> getOpcoes() {
        return opcoes;
    }
    public void setOpcoes(List<Opcao> opcoes) {
        this.opcoes = opcoes;
    }
}
  • And how’s your .xhtml?

  • @Luídne I have no idea how to create the <h:inputText> referencing that list. You don’t even need to have the .xhtml., I just need to know how the <h:inputText>.

2 answers

1

The method adicionarOpcao() adds a new option to the list with the data coming from <h:inputText>. After sending the form data (execute="@form") and the execution of the above method <f:ajax>, that was triggered by <h:commandButton>, updates the form (render="@form") with updated list data.

Controller

@ManagedBean
@ViewScoped
public class QuestaoBean {

    private List<Opcao> opcoes;
    private Opcao opcaoParaAdicionar;

    public QuestaoBean() {
         this.opcaoParaAdicionar = new Opcao();
    }

    public void adicionarOpcao() {
        if(opcoes == null) {
           opcoes = new ArrayList<>();
        }

        opcoes.add(opcao);
        opcao = new Opcao();
    }

    // getters e setters
}

View

<h:form>
    <ui:repeat value="#{questaoBean.opcoes}" var="opcao">
        <h:outputText value="#{opcao.nomeOpcao}" />
        </br>
    </ui:repeat>

    <h:inputText value="#{questaoBean.opcaoParaAdicionar.nomeOpcao}" />
    <h:commandButton value="Adicionar" action="#{questaoBean.adicionarOpcao}">
        <f:ajax render="@form" execute="@form" />
    </h:commandButton>
</h:form>

0

Below is a simple example:

<h:selectOneMenu value="#{opcao.idOpcao}">
    <f:selectItems value="#{questaoBean.opcoes}" 
                   var="opcao" 
                   itemValue="#{opcao.id}"
                   itemLabel="#{opcao.nameOpcao}" />
</h:selectOneMenu>
<h:commandButton value="Submit" action="bean.acao" />

This way the option object will instantiate only idOpcao, so in the bean.acao method there is a need for code to query the description of the option with its id.

The correct way would be to create an Opcaoconverter class that implements the Converter interface, allowing the full instantiation of 'option' and not just the 'idOpcao' attribute'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.