【问题标题】:getting value from an object in jstl, getting property name from a list从jstl中的对象获取值,从列表中获取属性名称
【发布时间】:2019-07-30 16:11:08
【问题描述】:

我是JSTL 的新手,我知道要获取字段值,我们可以使用以下代码:

<c:out value="${empDetails.id}" />
<c:out value="${empDetails.name}" />
<c:out value="${empDetails.dept}" />
<c:out value="${empDetails.locn}" />

我需要获取要在 UI 中的 arrayList 中显示的属性名称列表,在 JSP 中我想迭代并从 empDetails 中获取值。

我想写如下内容:

<c:forEach items="${list}" var="item">
    <c:out value="${empDetails}" property="${item}/>
</c:forEach>

列表将包含值“id”、“name”、“locan”、“dept”等。

有人可以帮忙怎么做吗?

【问题讨论】:

  • 感谢回复,对象名称是empDetails,我要打印的字段在列表中。我想将字段名称作为字符串传递。它类似于我们在 java 中使用反射的方式。在您的建议中,“id”是我需要在 jsp 中硬编码的字段名称。但我是动态得到的。

标签: arrays list jstl field jstl-functions


【解决方案1】:

我在其中一个 stackoverflow 问题中找到了答案,

<c:forEach items="${list}" var="item">
    <c:out value="${empDetails[item]}" />
</c:forEach>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    相关资源
    最近更新 更多