How to work with Table of Contents on Hymeleaf?

Asked

Viewed 1,999 times

2

Through a tutorial you can create an inclusion of products using the following html code

<c:forEach items="${tipos}" var="tipoPreco" varStatus="status">
            <div>
                <label>${tipoPreco}</label> 
                <input type="text" name="preco[${status.index}].valor">
                <input type="hidden" name="preco[${status.index}].tipo" value="${tipoPreco}">
            </div>
        </c:forEach>

Which is as follows;

inserir a descrição da imagem aqui

is linked to three entities

Product;

@ElementCollection
private List<Preco> preco;

Preco

private TipoPreco tipo;

And I love that it’s an Enum

EBOOS,IMPRESSO,COMBO;

I’m having a huge difficulty in performing the codes I did in JSP to do in Hymeleaf.

I know there is something in relation to th:each

<option th:each="status : ${todosStatusTitulo}" th:value="${status}" th:text="${status.descricao}"></option>

i made a few attempts, but were in vain, I need suggestions on how to put Thymeleaf code to have the same result as I did with JSP.

================================UPDATING============================

I made the following attempt following the suggested;

<div th:each="tipoPreco, status : ${tipos}">
    <label th:text="${tipoPreco}"></label> 
    <input type="text"  th:name="preco[${status.index}].valor"/> 
    <input type="hidden" th:name="preco[${status.index}].tipo" th:value="${tipoPreco}"/>
</div>

I would like to make it clear how the method that loads the form is;

@RequestMapping("form")
    public ModelAndView form(){
        ModelAndView modelAndView = new ModelAndView("produtos/form");
        modelAndView.addObject("tipos", TipoPreco.values());

        return modelAndView;
    }

And before the change made generated this error on the page;

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Apr 10 06:48:06 BRT 2016
There was an unexpected error (type=Internal Server Error, status=500).
Could not parse as expression: "preco[${status.index}].valor" (produtos/form:30)

And that was the error message on the consoles;

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "preco[${status.index}].valor" (produtos/form:30)
    at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:238) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:79) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:40) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor.getTargetAttributeValue(AbstractStandardSingleAttributeModifierAttrProcessor.java:65) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor.getModifiedAttributeValues(AbstractSingleAttributeModifierAttrProcessor.java:59) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor.processAttribute(AbstractAttributeModifierAttrProcessor.java:62) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.Node.processNode(Node.java:972) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]

He’s saying with this error message that he doesn’t recognize this chunk of code as a regular expression of Thymeleaf.

<input type="text"  th:name="preco[${status.index}].valor"/>

Maybe Thymeleaf’s approach to treating index is different from JSTL in JSP.

======================ANOTHER ATTEMPT============================

I found this link

http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#the-springstandard-dialect

and found this example;

<table>
            <tbody>
              <tr th:each="row,rowStat : ${sb.rows}">
                <td th:text="${rowStat.count}">1</td>
                <td th:text="${row.variety.name}">Thymus Thymi</td>
                <td th:text="${row.seedsPerCell}">12</td>
              </tr>
            </tbody>
          </table>

and tried to do so;

<table>
    <tbody>
      <tr th:each="tipoPreco,rowStat : ${tipos.rows}">
        <td th:text="${rowStat.count}">1</td>
        <td th:text="${row.preco.valor}">Thymus Thymi</td>
        <td th:text="${row.preco.tipo}">12</td>
      </tr>
    </tbody>
  </table>

And made that mistake;

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 6): Property or field 'rows' cannot be found on object of type 'br.com.casadocodigo.model.TipoPreco[]' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224) ~[spring-expression-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94) ~[spring-expression-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46) ~[spring-expression-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:374) ~[spring-expression-4.2.5.RELEASE.jar:4.2.5.RELEASE]

============================SECOND UPDATE===============

I put my code this way;

<tbody>
                <tr th:each="tipoPreco, row : ${tipos}">
                    <td>
                    <input th:text="${tipoPreco}"/>
                    </td>
                </tr>
            </tbody>
        </table>

And when generating the input s I tried to register this record and I did not succeed in inserting records in the database in the ebook, in the printed and combo, because it only presents the name of the values that are in the Enum class, I have to have a way to access the price class in order to enter the values

see how you’re doing in the browser

inserir a descrição da imagem aqui

3 answers

2


When using the th:each Thymeleaf offers a mechanism for you to control the iteration, the status variable. It is declared after the iteration variable.

To status variable contains some information such as: index, count, size, current and others you can look at here.

In your Github source code, you’re trying to iterate an Enum array. So the methods or attributes are not available so the error happens:

property or field 'rows' cannot be found on object of type 'br.com.casadocodigo.model.TipoPreco[]' - maybe not public?

I traded your code for this:

<table>
    <tbody>
        <tr th:each="tipoPreco, row : ${tipos}">
            <td th:text=" ${row.count}">

            </td><td th:text="${tipoPreco}"></td>
        </tr>
    </tbody>
</table>

And resulted in the exit:

inserir a descrição da imagem aqui

Along those lines <tr th:each="tipoPreco, row : ${tipos}"> declared first the iteration variable and then the status variable for iteration manipulation.

I recommend you read to documentation of Thymeleaf. In PDF it has just over 85 pages.

  • i updated my post.

  • That’s where the registration part comes in and not working with th:each. You’ll need to take a closer look at your code to see the problem. And what error occurred when you tried to register?

  • he only registered title, description and pages in the database.

  • Spring MVC is failing to bind, the way you want it, from the form to your business entity.

  • that’s exactly what’s going on.

  • 1

    I recommend you close this question by marking some answer as accepted, because it is the problem with index, and open a question about Bind forms with Spring MVC.

Show 1 more comment

1

Only the static text of the th:name with the dynamic value coming from the status. The correct code would be this:

 <div th:each="tipoPreco, status : ${tipos}">
    <label th:text="${tipoPreco}"></label>
    <input type="text" th:name="'preco['+${status.index}+'].valor'">
    <input type="hidden" th:name="'preco['+${status.index}+'].tipo'" th:value="${tipoPreco}">
</div>

0

<div th:each="tipoPreco, status : ${tipos}">
    <label th:text="${tipoPreco}"></label>
    <input type="text" th:name="'preco['${status.index}'].valor'">
    <input type="hidden" th:name="'preco['${status.index}'].tipo'" th:value="${tipoPreco}">
</div>
  • I just updated the post, could you take a look, please!

  • I did a second update.

  • Try this modelAndView.addObject("tipos", new ArrayList(TipoPreco.values()));

  • just modify the controller and leave it the way you suggested above in your last suggestion post?

  • @RequestMapping("form")&#xA; public ModelAndView form(){&#xA; ModelAndView modelAndView = new ModelAndView("produtos/form");&#xA; modelAndView.addObject("tipos", new ArrayList(TipoPreco.values()));&#xA;&#xA; return modelAndView;&#xA; }

  • It does not allow such modification in the controller because in the products table there is already a list with the price attribute as list.

  • My project is not in completion phase, actually I created the day I made this post. want me to post my project on Github for you to take a look?

  • Yes. It’ll make it easier to help you

  • my project is here >>> https://github.com/wladimirbandeira/casadocodigo/tree/master/casadocodigo

  • if you have difficulty importing the project, please let me know

  • any result?

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.