我正在尝试使用JSP动态生成内容。
我有一个<c:forEach>循环,可以在其中动态创建bean访问器。骨架类似于:
<c:forEach>
<c:forEach var="type" items="${bean.positionTypes}"> ${bean.table} // append 'type' to the "table" property </c:forEach>
我的问题是:我想更改${bean.table}基于类型。例如,如果类型是{"Janitor", "Chef},我想产生:
${bean.table}
{"Janitor", "Chef}
${bean.tableJanitor} ${bean.tableChef}
我该如何实现?
您可以使用大括号符号[]通过动态键访问bean属性。
[]
${bean[property]}
因此,根据您的示例:
<c:forEach var="type" items="${bean.positionTypes}"> <c:set var="property" value="table${type}" /> ${bean[property]} </c:forEach>