How to work with content in thymeleaf?

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>

That looks like this;

Itislinkedtothreeentities

Product;

@ElementCollectionprivateList<Preco>preco;

ByPrice

privateTipoPrecotipo;

ETypeofThickwhichisanenum

EBOOS,IMPRESSO,COMBO;

I'mhavingahardtimerealizingthecodesI'vemadeinJSPtodointhymeleaf.

Iknowthere'ssomethinginrelationtoth:each

<optionth: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 I could put the thymeleaf code to have the same result as I did with JSP.

================================ UPDATE ============== ==============

I made the following attempt following the suggested suggestion;

<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'd like to make it clear how the form loads the form;

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

        return modelAndView;
    }

And because of the change made, this error occurred 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 is saying with this error message that he does not recognize this snippet of code as a regular expression of thymeleaf.

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

Perhaps the thymeleaf approach to handling index is different from the JSTL of the JSP.

MORE TRY ==================== ======

I found this link

link

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 gave this error;

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]

========================== UPDATE ===============

I put my code that 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 was not successful in inserting records in the database in the ebook, print and combo, because it only presents the name of the values that are in the enum class, I have which have a way to access the class price to be able to enter the values

See how you're getting in the browser

    
asked by anonymous 10.04.2016 / 00:27

3 answers

2

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

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

In your source code that is in GitHub, you are trying to iterate through an Enum Array. Soon 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've changed 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 output:

Inthisline<trth:each="tipoPreco, row : ${tipos}"> I will first declare the iteration variable and then the status variable for iteration manipulation.

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

    
13.04.2016 / 01:40
1

You just needed to concatenate the static text of 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>
    
16.10.2017 / 18:34
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>
    
10.04.2016 / 01:34