Kleber Mota
Kleber Mota

Reputation: 9065

Access to class attributes with jstl

In my spring project, my view receive from controller a Map object like this:

Map<String, List<?>>

which I access in my jsp code this way:

                <c:forEach var="field" items="${values[item]}">
                    <c:out value="${field}"/> <br/>
                </c:forEach>

Considering the class indicatd by ? it's a regular POJO class, how I can access the attributes from this class in my jsp? In other words, what the correct instruction I should use to replace:

                    <c:out value="${field}"/> <br/>

because with this I am getting something like that when I open the page in the browser:

com.spring.loja.model.categoria.persistence.model.Categoria@41c0e228

UPDATE

I try use this, following answer posted in this topic:

<c:out value="${field.name}"/>

but I wonder if there is a way to use this method instead:

@Override
protected String getArgument(int ordem) {
    switch(ordem) {
     case 0: return "Id";
     case 1: return "Login";
     case 2: return "Senha";
     case 3: return "Nome";
     case 4: return "Sobrenome";
     case 5: return "E-Mail";
     case 6: return "Autorizacao";
     default: return null;
     }
}

and this way be able to avoid the use of the name of the getter method (It's a generic jsp page, used by several views, and I don't know which method will be used)

Upvotes: 5

Views: 7482

Answers (4)

Kleber Mota
Kleber Mota

Reputation: 9065

How I finally solve this:

I add this two methods in my service class:

public Map<String, List<String>> getListaAtributos() {
...
}
public Map<String, List<?>> getListaValores() {
...
}

the key for both methods it's the atributes from my class, and the value associated to them is: null, if the atribute have a primitive type, or a list of atributes of the class for the first one, and a list of values stored in database in this entity.

I pass this Map's to my view and use this way:

<c:forEach var="atributo" items="${map[item]}">
...
    <form:label path="${item}.${atributo}" class="form-control">${atributo}</form:label>
    <form:input path="${item}.${atributo}" class="form-control"/>
...
</c:forEach>

Upvotes: 0

Prasad
Prasad

Reputation: 3795

You can try with:

    <c:forEach var="myObj" items="${values[item]}">
        <c:if test="${not empty myObj.class.declaredFields}">
            <c:forEach var="field" items="${myObj.class.declaredFields}">
                <!--To catch NoSuchFieldException,SecurityException-->
                <c:catch>Field Name:${field.name} - Field Value:${myObj[field.name]}</c:catch>          
            </c:forEach>
        </c:if>
    </c:forEach>

Java equivalent of this is:

    if(myObj.getClass().getDeclaredFields() != null){
        for(Field field : myObj.getClass().getDeclaredFields()){
            System.out.println("Field Name:"+field.getName());
            System.out.println("Field value:"+field.get(object));
        }
    }

This will display fine as long as MyObj has simple data types as fields. If it has say a List myList, it will display as: myList[str1, str2].

Upvotes: 1

mike_m
mike_m

Reputation: 1546

If this POJO has for an example getName() getter, then you can access name field using:

<c:out value="${field.name}"/>

If you use Servlet +3.0 version, then you can invoke method from EL. Then you can try something like that:

<c:out value="${field[field.getArgument(2)]}"/>

Upvotes: 3

geoand
geoand

Reputation: 63991

If you are using EL 2.2 then you could use

<c:out value="${field.getArgument(1)}"/>

Refer to this SO answer for more details

Upvotes: 1

Related Questions