Display data from an ArrayList in a DataTable in Java?

0

What I want to do is show the data that I put in my textBox on my page first I send them to a class called people, I keep them in a ArrayList and then I consult them to be shown by a REGISTER method that is in PersonaBean in a dataTable with the register button. The problem is that when I give it to register it does not show them on my page. I put the code below!

INDEX
<?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:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>


                <h:panelGrid columns="3">
                    <h:outputLabel value="Nombre"/>
                    <h:inputText id="txtNombre" value="#{personaBean.persona.nombre}" required="true" label="Nombre requerido"/>
                    <h:message for="txtNombre" style="color: red"/>

                    <h:outputLabel value="Edad"/>
                    <h:inputText id="txtEdad" value="#{personaBean.persona.edad}" required="true" label="(1-18)" >
                        <f:validator validatorId="mayorDeEdad"/>
                    </h:inputText>
                    <h:message for="txtEdad" style="color: red"/>

                    <h:outputLabel value="Sexo"/>
                    <h:inputText id="txtSexo" value="#{personaBean.persona.sexo}" validator="#{personaBean.validar}"/>
                    <h:message for="txtSexo" style="color: red"/>
                </h:panelGrid>

            <h:commandButton actionListener="#{personaBean.registrar()}" value="Registrar"/>
            <h:commandButton actionListener="#{personaBean.registrar()}" value="Registrar SIN VALIDAR" immediate="true"/>    

            <h:dataTable value="#{personaBean.lstPersona}" var="per" rendered="#{personaBean.lstPersona.size() > 0 eq true}">
                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Nombre"/>
                    </f:facet>  
                    <h:outputText value="#{per.nombre}"/>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Edad"/>
                    </f:facet>    
                    <h:outputText value="#{per.edad}"/>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Sexo"/>
                    </f:facet>    
                    <h:outputText value="#{per.sexo}"/>
                </h:column>
            </h:dataTable>            
        </h:form>
    </h:body>
</html>

PERSONAL CLASS

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

/**
 *
 * @author kali
 */
public class Persona {
    private String nombre;
    private int edad;
    private String sexo;

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getEdad() {
        return edad;
    }

    public void setEdad(int edad) {
        this.edad = edad;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }


}

CLASE PERSONA BEAN
package beans;

import clases.Persona;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

/**
 *
 * @author kali
 */
@Named(value = "personaBean")
@Dependent
public class PersonaBean {
    private Persona persona = new Persona();
    private List<Persona> lstPersona = new ArrayList();

    public PersonaBean() {
    }

    public Persona getPersona() {
        return persona;
    }

    public void setPersona(Persona persona) {
        this.persona = persona;
    }

    public List<Persona> getLstPersona() {
        return lstPersona;
    }

    public void setLstPersona(List<Persona> lstPersona) {
        this.lstPersona = lstPersona;
    }

    public void registrar(){
        this.lstPersona.add(this.persona);
    }


    public void validar(FacesContext context,UIComponent toValidate, Object value){

    context = FacesContext.getCurrentInstance();

    String texto = (String)value;

    if(!texto.equalsIgnoreCase("M" ) && !texto.equalsIgnoreCase("F")){

            ((UIInput)toValidate).setValid(false);
            context.addMessage(toValidate.getClientId(context), new FacesMessage("Sexo NO VALIDO"));
        }
    }

}
    
asked by Giorgio Magaña 30.05.2017 в 07:51
source

1 answer

0
@Named(value = "personaBean")
@Dependent
public class PersonaBean {

The managed beans have defined a scope or scope that defines how to share (or not) instances. In this case you have defined @Dependent , which means

  
  • No injected instance of the bean is ever shared between multiple injection points.
  •   
  • Any instance of the bean injected into an object that is being created by the container is bound to the lifecycle of the newly created object.
  •   
  • When a Unified EL expression in a JSF or JSP page that refers to the bean by its The name is evaluated, at most one instance of the bean is instantiated. This instance exists to service just a single evaluation of the EL expression. It is reused if the bean EL expression is evaluated. The expression, but is never reused when the expression is evaluated again, or when another EL expression is evaluated.
  •   
  • Any instance of the bean that receives a producer method, producer field, disposer method or observer method invocation exists to service that invocation only.
  •   
  • Any instance of the bean injected into method parameters of a disposer method or observer method exists to service the method invocation only.
  •   

In this case, the first and third points are of interest, which means that if you have:

#{miBean.cantidadAzules + miBean.cantidadRojas}

only one instance is created, but if you have

#{miBean.cantidadAzules + miBean.cantidadRojas}<br/>
#{miBean.nombre}

one instance is created for the first expression and another for the second.

If this is the case for accessing the bean on the same page / request ( request ), also in each request different bean will be created , with which your bean is always "newly created" .

The solution is to change the scope from @Dependent to javax.faces.view.@ViewScoped (JSF2.2) or javax.enterprise.context.SessionScoped .

If you later have doubts about scope s, it is usually useful to add a log message to the constructor to see where (and above all, how many times) instances are being created. This question in English explains the different scope s in detail

    
answered by 30.05.2017 в 10:04