How to use Thymeleaf each?

Asked

Viewed 2,140 times

4

Observe the code well in 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 above annotation it is possible to take 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 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.

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

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

and is 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 made that mistake;

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]
  • I made my attempts, they didn’t need to put them as -1

  • +1 for the effort to ask the question with much information.

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.

  • Depending on how you use Thymeleaf, it requires all tags to be closed.

  • I just updated my post, take a look.

  • So in this case, close the input tag, leaving: <input type="text" /> and not as it was, </input>

1

Good Morning,

Try it like 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 hard on HTML formatting.

1

First, is the name of your object in the Model "types" or "status"? If "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>

Reporting two parameters (t,i) on th:each, Thymeleaf will return the map object and its key (index).

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

Browser other questions tagged

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