如何使用Th:每个

发布于 2025-01-25 13:15:28 字数 592 浏览 4 评论 0原文

这是来自Spring MVC控制器的JsonObject列表。

List<JSONObject> jsonDataList =
 
[{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}, {"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}]

如何使用每个:每个:迭代胸腺中的jsonobject列表?

下面的HTML文件中的代码:=&gt;

 <tr th:each="data: ${jsonDataList}">   
    <td align="center"><span th:text="${data.key1}"></span></td>   // getting exception here                 
 </tr>

得到例外为: 引起:org.attoparser.parseexception:评估弹簧表达式的异常:“ data.key1”

Here is that list of JSONObject which is coming from Spring MVC Controller.

List<JSONObject> jsonDataList =
 
[{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}, {"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}]

How to Iterate List of JSONObject in Thymeleaf using th:each?

Code IN HTML FILE below:=>

 <tr th:each="data: ${jsonDataList}">   
    <td align="center"><span th:text="${data.key1}"></span></td>   // getting exception here                 
 </tr>

Getting Exception as :
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "data.key1"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2025-02-01 13:15:28

这是一种方法,但它做出了一些假设:

a)每个JSON对象具有相同数量的条目(否则您可以有一个破烂的表,每行中包含不同数量的单元格)。

b)每个JSON对象都以相同的顺序具有相同的键(如果您希望表具有一致的列标题)。

同样,问题中的示例JSON假定所有值都是字符串(value1等)。如果您的JSON值中有不同类型的对象,则需要确保它们具有所需的字符串表示(例如使用toString())。

方法:

<table>
    <tr> 
        <th:block th:each="heading : ${jsonDataList.get(0).names()}">
            <th th:text="${heading}"></th>
        </th:block>
    </tr>
            
    <tr th:each="item : ${jsonDataList}">
        <th:block th:each="name : ${item.names()}">
            <td th:text="${item.get(name)}"></td>              
        </th:block>
    </tr>
</table>

第一个&lt; tr&gt;部分负责从列表中的第一个对象读取JSON对象键:

${jsonDataList.get(0).names()}

最终&lt; tr&gt; e节是相似的,但使用查找其相关值的关键:

${item.get(name)}

由此产生的HTML为您提供了一个简单的表:

<table>
    <tr>                 
        <th>key1</th>                
        <th>key2</th>                
        <th>key3</th>                
        <th>key4</th>                
    </tr>            
    <tr>                
        <td>value1</td>                              
        <td>value2</td>                              
        <td>value3</td>                              
        <td>value4</td>                              
    </tr>            
    <tr>                
        <td>value1</td>                              
        <td>value2</td>                              
        <td>value3</td>                              
        <td>value4</td>              
    </tr>
</table>


参考:

th:block标签记录在在这里

可用于jsonobject的方法已记录在这里

Here is one approach, but it makes some assumptions:

a) Each JSON object has the same number of entries (otherwise you could have a ragged table, containing different numbers of cells in each row).

b) Each JSON object has the same keys in the same order (if you want the table to have consistent column headings).

Also, the sample JSON in the question assumes all values are strings (value1 and so on). If you have different types of objects in your JSON values, then you will need to ensure they have the required string representations (e.g. using toString()).

The approach:

<table>
    <tr> 
        <th:block th:each="heading : ${jsonDataList.get(0).names()}">
            <th th:text="${heading}"></th>
        </th:block>
    </tr>
            
    <tr th:each="item : ${jsonDataList}">
        <th:block th:each="name : ${item.names()}">
            <td th:text="${item.get(name)}"></td>              
        </th:block>
    </tr>
</table>

The first <tr> section handles reading the JSON object keys from the first object in the list:

${jsonDataList.get(0).names()}

The final <tr> section is similar, but uses the keys to look up their related values:

${item.get(name)}

The resulting HTML gives you a simple table:

<table>
    <tr>                 
        <th>key1</th>                
        <th>key2</th>                
        <th>key3</th>                
        <th>key4</th>                
    </tr>            
    <tr>                
        <td>value1</td>                              
        <td>value2</td>                              
        <td>value3</td>                              
        <td>value4</td>                              
    </tr>            
    <tr>                
        <td>value1</td>                              
        <td>value2</td>                              
        <td>value3</td>                              
        <td>value4</td>              
    </tr>
</table>


References:

The th:block tag is documented here.

The methods available to be used for JSONObject are documented here.

别理我 2025-02-01 13:15:28

怎么样?

<tr th:each="data: ${jsonDataList}">   
  <td align="center"><span th:text="[[${data.key1}]]"></span></td> 
</tr>

how about this?

<tr th:each="data: ${jsonDataList}">   
  <td align="center"><span th:text="[[${data.key1}]]"></span></td> 
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文