简体   繁体   English

尝试在Thymeleaf中使用动态字段

[英]Trying to use dynamic fields in Thymeleaf

In themeleaf tutorial they teach you how to extract values from a list using two levels of variables, like this [[${variable1[__${variable2}__]}]] but the point is that i want to extract the value using: [[${variable1[${variable2[__${variable3}__]}]}]] but I don't know how to type this code correctly. 在themeleaf教程中,他们教您如何使用两个级别的变量从列表中提取值,例如[[${variable1[__${variable2}__]}]]但要点是我想使用以下方法提取值: [[${variable1[${variable2[__${variable3}__]}]}]]但我不知道如何正确键入此代码。

I am trying to create a HTML element using objects that the Spring server is giving with mModelAndView.addObject("project",Projects.findById(pid)); 我正在尝试使用Spring服务器通过mModelAndView.addObject("project",Projects.findById(pid));给定的对象来创建HTML元素mModelAndView.addObject("project",Projects.findById(pid)); and mModelAndView.addObject("local",posto); mModelAndView.addObject("local",posto); where Projects and posto are java classes with different variables. 其中Projects和posto是具有不同变量的Java类。

So, if I write this peace of code, all is fine and correct : 因此,如果我编写这种代码,那么一切都很好并且是正确的:

HTML & Thymeleaf Code in the page.html: page.html中的HTML和Thymeleaf代码:

 <div th:each="parametro,row1 : ${project.Lista}"> <p class="info">[[${parametro.pergunta}]]: [[${parametro.rrespostas[0]}]], [[${local.lista_respostas[__${row1.index}__]}]] </p> </div> 

So, all the variables are well understood by themeleaf and this is the result: 因此,它们易于理解所有变量,这就是结果:

代码结果

But the point is that i want that the [[${local.lista_respostas[__${row1.index}__]}]] variable will be the [[${parametro.rrespostas[0]}]] index (the rrespostas variable contains a number), that means that I want to substitute this '0' with the rrespostas value. 但是关键是我希望[[${local.lista_respostas[__${row1.index}__]}]]变量将是[[${parametro.rrespostas[0]}]]索引( rrespostas变量包含一个数字),这意味着我想用rrespostas值替换此“ 0”。

I tried with : 我尝试了:

  <p class="info">[[${parametro.pergunta}]]: [[${parametro.rrespostas[__${local.lista_respostas[__${row1.index}__]}__}]] </p> <p class="info">[[${parametro.pergunta}]]: [[${parametro.rrespostas[[[${local.lista_respostas[__${row1.index}__]}]]]}]] </p> 

And i tried more combinations trying to extract the information but nothing works. 我尝试了更多组合以尝试提取信息,但是没有任何效果。

Any idea to do that in themeleaf? 有什么想法可以在他们那里做吗?

I am not sure that you can make this work. 我不确定您可以完成这项工作。

This does not directly fix your problem but: In my opinion the only clean way is to start using Processors. 这不能直接解决您的问题,但是:我认为唯一的干净方法是开始使用处理器。 It is not very hard to write your own and you have the huge benefit of being back in Java-Land where the compiler helps you and you can access all your libraries etc. 编写自己的代码不是很困难,并且拥有返回Java-Land的巨大好处,在Java-Land中编译器可以帮助您,并且您可以访问所有库等。

http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM