JSP EL:动态创建属性名称 [英] JSP EL: dynamic creation of property name

查看:100
本文介绍了JSP EL:动态创建属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSP动态生成内容。

I am trying to dynamically generate content using JSP.

我有一个< c:forEach> 我在其中动态创建bean访问器的循环。骨架类似于:

I have a <c:forEach> loop within which I dynamically create bean accessors. The skeleton resembles this:

<c:forEach var="type" items="${bean.positionTypes}">
    ${bean.table}  // append 'type' to the "table" property
</c:forEach>

我的问题是:我想更改 $ {bean.table} 基于类型。例如,如果类型是 {Janitor,Chef} ,我想生产:

My problem is: I want to change the ${bean.table} based on the type. For example, if the types were {"Janitor", "Chef}, I want to produce:

${bean.tableJanitor}
${bean.tableChef}

如何实现这一目标?

推荐答案

您可以使用括号符号 [] 使用动态密钥访问bean属性。

You can use the brace notation [] to access bean properties using a dynamic key.

${bean[property]}

所以,根据你的例子:

<c:forEach var="type" items="${bean.positionTypes}">
    <c:set var="property" value="table${type}" />
    ${bean[property]}
</c:forEach>

这篇关于JSP EL:动态创建属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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