5

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)

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
  • 1
    `` is calling `toString()` implicitly. What do you expect to see? – geoand Jul 09 '14 at 13:10
  • @geoand see the answer and my comment below to more details about what I want to do. – Kleber Mota Jul 09 '14 at 13:47
  • There is no such thing as ordering of methods in Java. I suggest that you have all your model objects implement a specific interface that has a method that returns the data you want – geoand Jul 09 '14 at 13:52
  • @geoand yes, I have that. See my update, where I include the method which return the data I want use. – Kleber Mota Jul 09 '14 at 13:54

4 Answers4

3

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)]}"/>

mike_m
  • 1,526
  • 4
  • 14
  • 19
  • Ok, this works, but I wonder if there is a way to do this without use the name of the getter method, but use the "position" of the method (0=Id, etc). I have a method in each one of my classes which it's a implementation of a abstract method returning the name of the method given this position. IS this possible? – Kleber Mota Jul 09 '14 at 13:45
1

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

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
1

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].

Prasad
  • 3,785
  • 2
  • 14
  • 23
0

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>
Kleber Mota
  • 8,521
  • 31
  • 94
  • 188