Not able to get List from inside bean in a JSP

I am trying to create print a list of strings on my jsp with the information in a bean called copa, stored in the session.

<jsp:useBean id="copa" class="Beans.CopaBean" scope="session"/>

                                <c:forEach var="item" items="${copa.mSelecciones}">
                                       ${item.mNombre}
                                    </c:forEach>

My object copa, implements java.io.Serializable. Inside it has a List of objects that do also implement this interface. I can also see the copa object being filled in the variables tab in NetBeans and the collection of mSelecciones with data inside..

I have tryied absolutely everything..from making the property mSelecciones public, to change the name to be shorter, to invoke the get method, getmSelecciones() ,etc but I have not been able to get my code to print any text on the html.

Any help?

This is the printout in the HTML when I run the code, as you can see I have the two objects sitting there..just cant iterate them.

 <c:forEach var="a" items="[Beans.SeleccionBean@7022efdf, Beans.SeleccionBean@5b252fd]">

                                    </c:forEach>

This is my CopaBean class structure :

    public class CopaBean implements java.io.Serializable {

    public  List<SeleccionBean> mSelecciones = null;


    public List<SeleccionBean> getmSelecciones() throws SQLException {
     }
}

This is my SeleccionesBean class structure :

    public class SeleccionBean  implements java.io.Serializable{
    private String mNombre;
    public String getmNombre() {
            return mNombre;
     }
}

Upvotes: 0

Views: 868

Answers (2)

cmd
cmd

Reputation: 11841

You are using JSTL, thus you must use JavaBean naming conventions for all methods you intend to use with JSTL.

For example, the statement <c:forEach var="item" items="${copa.mSelecciones}, fails because the java application server is attempting to resolve the method CopaBean.getMSelecciones() and cannot find it. Note that the M is capitalized. Also, note that the m in your method is not!

For the app server to resolve the method, you must ensure its name follows JavaBean naming conventions. e.g. getters and setters must be in the form getSomeMethod, setSomeMethod, respectively. Notice that the first letter following the get/set is capitalized.

To fix you code, change

public List<SeleccionBean> getmSelecciones() throws SQLException {

to

public List<SeleccionBean> getMSelecciones() throws SQLException {

You also have the same problem with SeleccionBean.getmNombre(). Change it to SeleccionBean.getMNombre().

Upvotes: 1

Frederic Lachasse
Frederic Lachasse

Reputation: 721

The JavaBean convention is to define getters and setters by using "set" or "get" followed by the name of the property with the first letter upper cased, i.e. for mSelecciones, the getter would be getMSelecciones(), not getmSelecciones(). Same with mNombre -> getMNombre()

Upvotes: 0

Related Questions