How to use thymeleaf each?

3

Look closely at the code on my controller;

@RequestMapping("/produtos/form")
    public ModelAndView form() {

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

        return modelAndView;

    }

This is my html page, and write the following annotation at the top of the page

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

With the annotation above you can pick up the list as follows;

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

There is nothing wrong with everything that has been described now, I just wanted to know how it would look in thymeleaf, the only thing that would change would be the html part, I just need to modify the html part.

===================================================== ========================

This was my attempt

<select>
    <table>
        <tr th:each="tipo : ${status}">
            <td></input type="text" name="precos[${status.index}].valor"></td>
            <td></input type="text" name="precos[${status.index}].tipo"></td>
        </tr>
    </table>
</select>

being that you are generating this error

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

Tue Apr 05 06:43:15 BRT 2016
There was an unexpected error (type=Internal Server Error, status=500).
Exception parsing document: template="produtos/form", line 31 - column 8

===================================================== ====================

Consoles

org.xml.sax.SAXParseException: O tipo de elemento tr" deve ser encerrado pela tag final correspondente "</tr>".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source) ~[na:1.8.0_65]

/////////////////////////////////////////////// ////////////////////////////

I tried this way;

<select>
            <table>
                <tr th:each="tipo : ${status}">
                    <td><input type="text" name="precos[${status.index}].valor"></td>
                    <td><input type="text" name="precos[${status.index}].tipo"></td>
                </tr>
            </table>
        </select>

and gave this error;

org.xml.sax.SAXParseException: O tipo de elemento input" deve ser encerrado pela tag final correspondente "</input>".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) ~[na:1.8.0_65]
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) ~[na:1.8.0_65]
    
asked by anonymous 05.04.2016 / 00:59

3 answers

1

The problem is apparently here:

< / input type="text" name="prices [$ {status.index}]." value

Change to:

< input type="text" name="prices [$ {status.index}]." value

The input tag does not need to be closed.

    
05.04.2016 / 13:02
1

Good morning,

Try this:

<select>
    <table>
        <tr th:each="tipo : ${status}">
            <td><input type="text" name="precos[${status.index}].valor"/></td>
            <td><input type="text" name="precos[${status.index}].tipo"/></td>
        </tr>
    </table>
</select>

Thymeleaf is very stiff with HTML formatting.

    
05.04.2016 / 13:31
1

First, the name of your object in the Model is "types" or "status"? If it is "types", use the following code:

<table>
  <tr th:each="t,i : ${tipos}">
    <td th:text="${i.index}"></td>
    <td th:text="${t.name()}"></td>
  </tr>
</table>

By entering two parameters (t, i) in th: each, Thymeleaf will return the map object and its respective index key.

As you use an enum class in this case, I called the .name () method to return the name of your variable. The .toString () method can also be called but then you would have to override it in your enum class.

    
05.04.2016 / 14:02