View all fields / properties of bean in JSP / JSTL

和自甴很熟 提交于 2019-11-29 20:27:50
Toby

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>&dollar;{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>&lt;% object.getName() %&gt;</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:&nbsp; ${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>:&nbsp; ${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>

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;
}

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}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!