View all fields / properties of bean in JSP / JSTL

試著忘記壹切 提交于 2019-11-28 16:29:56

问题


I have a bean, ${product}. I would like to view all of the available fields / properties of this bean. So for instance, ${product.price}, ${product.name}, ${product.attributes.colour} etc.

Is it possible to dynamically print out all names and values of these properties in JSP, using JSTL/EL?

Something like:

<c:forEach items="${product}" var="p">  
    ${p.key} - ${p.value}
</c:forEach>

回答1:


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>



回答2:


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>



回答3:


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



回答4:


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}


来源:https://stackoverflow.com/questions/16302554/view-all-fields-properties-of-bean-in-jsp-jstl

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