简体   繁体   中英

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.

I am trying to create a HTML element using objects that the Spring server is giving with mModelAndView.addObject("project",Projects.findById(pid)); and mModelAndView.addObject("local",posto); where Projects and posto are java classes with different variables.

So, if I write this peace of code, all is fine and correct :

HTML & Thymeleaf Code in the page.html:

 <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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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