I have a bean, ${product}
. I would like to view all of the available fields / properties of this bean. So for instance, ${product.price}
, ${p
Replace object with the bean to determine.
<c:set var="object" value="${product}" />
Display all declared fields and their values.
<c:if test="${not empty object['class'].declaredFields}">
<h2>Declared fields <em>${object.name}</em></h2>
<ul>
<c:forEach var="field" items="${object['class'].declaredFields}">
<c:catch><li><span style="font-weight: bold">
${field.name}: </span>${object[field.name]}</li>
</c:catch>
</c:forEach>
</ul>
</c:if>
Display all declared methods.
<c:if test="${not empty object['class'].declaredMethods}">
<h2>Declared methods<em><% object.getName() %></em></h2>
<ul>
<c:forEach var="method" items="${object['class'].declaredMethods}">
<c:catch><li>${method.name}</li></c:catch>
</c:forEach>
</ul>
</c:if>
Ready to use version of @Toby's answer
<p class="TODO <your name> PRINT OBJECT PROPERTIES">
<c:set var="object" value="${<your object here>}" />
<h2><b>Object: ${object.class} </b></h2>
<h3><b>Declared fields</b></h3>
<c:if test="${!empty object.class.declaredFields}">
<ul>
<c:forEach var="attr" items="${object.class.declaredFields}">
<c:catch><li><b>${attr.name}</b>: ${object[attr.name]}</li></c:catch>
</c:forEach>
</ul>
</c:if>
<c:if test="${empty object.class.declaredFields}">No declared fields</c:if>
<h3><b>Declared methods</b></h3>
<c:if test="${!empty object.class.declaredMethods}">
<ul>
<c:forEach var="attr" items="${object.class.declaredMethods}">
<c:catch><li><b>${attr.name}</b>(...)</li></c:catch>
</c:forEach>
</ul>
</c:if>
<c:if test="${empty object.class.declaredMethods}">No declared methods</c:if>
</p>
What you want to do is basically void of sense, as it is you who must know beforehand what the object is and what are its fields. If for some reason you still want to proceed, you can have a method that returns what you want:
public Map<String, Object> getProperties() {
//create your map and populate it via plain strings-getters for fields
//or use Java Reflection API
}
With this method your map will be accessible with
${bean.properties}
There is nothing called JSTL Object. JSTL just provides the way to access java objects in JSP in cleaner and more readable way(other way is scriplet). Just implement toString(here is the link stating brief about toString method http://www.javatpoint.com/understanding-toString()-method) method in your java object in this case product and now
1)If your question is how to print object properties in java
System.out.println(product)
2)To print it in JSP
${product}
Here is toString method for your case
public String toString(){
return price + name;
}