在JSP/JSTL中查看bean的所有字段/属性 [英] View all fields / properties of bean in JSP / JSTL

查看:60
本文介绍了在JSP/JSTL中查看bean的所有字段/属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个豆,${product}.我想查看此bean的所有可用字段/属性.因此,例如${product.price}${product.name}${product.attributes.colour}

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.

是否可以使用JSTL/EL在JSP中动态打印出这些属性的所有名称和值?

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

类似的东西:

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

推荐答案

将对象替换为Bean来确定.

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>

显示所有声明的方法.

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

这篇关于在JSP/JSTL中查看bean的所有字段/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆