动态struts2对象字段名称 [英] Dynamic struts2 object field name

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

问题描述

我正在尝试动态生成一个包含其列的表。

I'm trying to generate a table with its columns dynamically.

目前,这是我的代码:

<s:iterator value='entities' var="entity">
  <tr>
    <s:iterator value="beanMD.fieldsMetaData" var="fieldMD" >
      <td><s:property value="#entity.email"/> <!-- Prints OK :  email@email.com --> 
      <s:property value="#fieldMD.value.name"/> <!-- Prints OK :  email-->

       <s:property value="#entity.%{#fieldMD.value.name}" /> <!--  Nothing -->
       <s:property value="#entity.%{fieldMD.value.name}" /> <!--  Nothing -->
       <s:property value="entity.%{fieldMD.value.name}" /> <!--  Nothing -->
       <s:property value="entity.%{#fieldMD.value.name}" /> <!--  Nothing -->

     </td>
   </s:iterator>
  </tr>
</iterator>




  • 实体它是带有数据的bean的集合。

    • entities it's the collection with the beans with the data.

      beanMD.fieldsMetadata 是一个Map,其中包含实体的属性。

      beanMD.fieldsMetadata is a Map, which contains the attributes of the entities.

      在上面的示例中,我使用属性email作为示例。如果我直接询问属性,则返回OK。如果我问列名,那也没关系。
      但我找不到一种方法可以一起使用。

      In the example above, I used the attribute "email" as an example. If I ask directly the attribute, it returns OK. If I ask the column name, it also is OK. But I can´t find a way to use the together.

      这篇文章似乎有关,
      具有嵌套迭代器动态列和行的Struts2
      但它使用了所有地图,并且没有'帮助我。

      This post seems related, Struts2 with Nested Iterator dynamic column and rows but it uses all Maps, and didn't help me much.

      推荐答案

      使用方括号从bean中获取值。

      Use square brackets to get value from the bean.

      <s:property value="#entity[#fieldMD.value.name]" />
      

      这是可能的,因为内部OGNL将以下表达式视为完全相同。

      It is possible because internally OGNL treats following expressions exactly the same.

      所以

      <s:property value="date.time" />
      

      <s:property value="date['time']" />
      

      产生相同的结果。

      这篇关于动态struts2对象字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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