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;
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
i updated my post.
– wladyband
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?
– adelmo00
he only registered title, description and pages in the database.
– wladyband
Spring MVC is failing to bind, the way you want it, from the form to your business entity.
– adelmo00
that’s exactly what’s going on.
– wladyband
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.
– adelmo00