Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 235
Kleber Mota Avatar asked Jul 09 '14 13:07

Kleber Mota


People also ask

How do I access attributes of a class in Java?

You can access attributes by creating an object of the class, and by using the dot syntax (. ): The following example will create an object of the Main class, with the name myObj.

What is JSTL in JSP?

The JSP Standard Tag Library (JSTL) represents a set of tags to simplify the JSP development. Fast Development JSTL provides many tags that simplify the JSP. Code Reusability We can use the JSTL tags on various pages.

What are JSTL formatting tags and how to use them?

JSTL formatting tag library provides a convenient way for formatting text, numbers, dates, times and other variables for better display. JSTL formatting tags can also be used to enhance the internationalization of a website. Before using these formatting tags, we've to add the taglib to our JSP:

How do I create a class with attributes?

Create a class called " Main " with two attributes: x and y: Another term for class attributes is fields. You can access attributes by creating an object of the class, and by using the dot syntax (. ): The following example will create an object of the Main class, with the name myObj.


1 Answers

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

like image 122
mike_m Avatar answered Sep 25 '22 05:09

mike_m