show Arraylist in jsf

0

Hello everyone, I'm starting JSF and I've run into a problem that's driving me crazy.

I have a class info_person. Another PersonaBean class. A page xhtml ShowTablaPersonas.xhtml.

The final intention is that you connect to a database and read all the records in the database, put it in an ArrayList, then show this arrayList in the xhtml page.

But I wanted to start with something simpler. to see if the mechanism works. here the code that I have

public class Info_Persona {
    private int num1;
    private int num2;
    private int num3;


    public Info_Persona(int num1, int num2, int num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;       
    }

    public int getNum1() {
        return num1;
    }


    public void setNum1(int num1) {
        this.num1 = num1;
    }


    public int getNum2() {
        return num2;
    }


    public void setNum2(int num2) {
        this.num2 = num2;
    }


    public int getNum3() {
        return num3;
    }


    public void setNum3(int num3) {
        this.num3 = num3;
    }


    @Override
    public String toString() {
        return "Info_Persona [num1=" + num1 + ", num2=" + num2 + ", num3=" + num3 + "]";
    }
}

For the ManagedBean class in faces-config.xml

<managed-bean>
    <managed-bean-name>PersonaBean</managed-bean-name>
    <managed-bean-class>paq_Persona.PersonaBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

PersonaBean.java

public class PersonaBean {
static List<Info_Persona> listaPersona = new ArrayList<>();

public void listarPers() {

    int num1 = 0;
    int num2 = 1;
    int num3 = 2;

    while (num1 < 3) {
        Info_Persona per = new Info_Persona(num1, num2, num3);
        listaPersona.add(per);
        num1++;
        num2++;
        num3++;

    }

}

}

The xhtml page: ShowPersona.xhtml

<?xml version="1.0" encoding="UTF-8"?>

    

                      

<h1>JSF 2.0 + JDBC Example</h1>

<h:dataTable  value="#{personaBean.listaPersona()}" var="tempPers"
    styleClass="order-table" headerClass="order-table-header"
    rowClasses="order-table-odd-row,order-table-even-row">

    <h:column>
        <f:facet name="header">
                num1
            </f:facet>
                #{tempPers.num1}
        </h:column>

    <h:column>
        <f:facet name="header">
                num2
            </f:facet>
                #{tempPers.num2}
        </h:column>

    <h:column>
        <f:facet name="header">
                num3
            </f:facet>
                #{tempPers.num3}
        </h:column>
</h:dataTable>

    
asked by piter_1 08.06.2018 в 17:07
source

1 answer

0

In the value property of the h: datatable component, you should pass it a collection, something that can be iterated. Your method in the controller does not return anything (it's void). That you do a while or a for in the controller does not mean that it will iterate in the view. JSF does not work like this. The local variables that you have in your method ( num1, num2, num3 ) are not known by the view, they are local to the method, after they are executed they are lost, like any local variable.

JSF is a component-based framework, server-side, that implements a pattern called MVC, Model-View-Controller.

  • View : represented by the .xhtml pages
  • Controllers : the managed bean, where the presentation logic resides
  • Model : all information that the view will show, such as a Person.

As you are starting, we are going to make a simple example that in your case should work (then you can adapt it to work with your classes)

Persona.java

    public class Persona {

        private String nombre;
        private String apellido;

        public Persona(String nombre, String apellido) {

            this.persona = persona;
            this.apellido = apellido;
        }

        public void setNombre(String nombre) {

            this.nombre = nombre;
        }

        public String getNombre() {

            return nombre;
        }

        public void setApellido(String apellido) {

            this.apellido = apellido;
        }

        public String getApellido() {

            return apellido;
        }   
    }

This is going to be my Model. It has the respective getters and setters so that from the view it can refer to these attributes. Later I'll show you how.

Now I add a method to your managed bean to return a list of people

    public List<Persona> getPersonas() {

        /* Esta inicialización es a modo de ejemplo. Los datos probablemente 
        los obtengas de una base de datos o un web service, pero a modo de 
        ejemplo sirve */
        List<Personas> personas = new LinkedList<Persona>();

        personas.add(new Persona("Diego", "Maradona"));
        personas.add(new Persona("Leonel", "Messi"));
        personas.add(new Persona("Crsitiano", "Ronaldo"));

        return personas;
    }

Note that this method is called getPersonas . Now, to be able to access from the list view, I'm going to do something like this: # {personaBean.personas} . By convention, JSF will invoke the getPersonas () method. Using setters and getters is very common in many frameworks, not just JSF. MyBatis, Hibernate, Spring, Spring-mvc to name a few.

Finally, I will make a modification in how you draw the datatable:

    <h:dataTable  value="#{personaBean.personas}" var="persona"
        styleClass="order-table" headerClass="order-table-header"
        rowClasses="order-table-odd-row,order-table-even-row">

        <h:column>
            <f:facet name="header">Nombre</f:facet>
            #{persona.nombre}
        </h:column>

        <h:column>
           <f:facet name="header">Apellido</f:facet>
           #{persona.apellido}
        </h:column>
    </h:dataTable>

Notice how var declare the person variable (I could have put any other name). What kind will be a person? Well it will be the type that contains the collection. These types of frameworks use the API Reflection a lot. See how I access the properties of the Persona class through its getters: # {person.name} and # {person.lastname}

Well I hope it's useful and any questions you ask!

    
answered by 09.06.2018 в 01:39