Problem inserting a form from a fragment
See original GitHub issueI’ve used Thymeleaf 3.0.11, and spring boot 2.3.0
I’ve put the problem on stackoverflow problem, no one responded: https://stackoverflow.com/questions/62694597/create-a-fragment-for-a-form-in-thymeleaf-and-spring
Putting the 2 fragments below in the same file didn’t work, kept throwing: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘strada’ available as request attribute So the fix was to move the fragments to different files and the problem disappeared but the rendering takes 4s which is quite huge ( the loss in performance comes from rendering combos, cause if i eliminate them the rendering takes 50ms). As long as they are in the same file the problem persist and rarely it only shows on the logs.
fragment 1:
<div th:fragment="form(adresa)" >
<form action="#" th:action="@{/adresa}" th:object="${adresa}" method="post">
<input type="hidden" th:field="*{id}">
<table>
<tr><td>Strada: </td><td><input type="text" th:field="*{strada}"></td></tr> <--error always here
......
</table>
<p>
<input type="submit" value="Save"/>
<input type="reset" value="Reset">
</p>
</form>
</div>
fragment 2:
<table th:fragment="table(list)" border="1">
<tr>
<th>.</th>
<th>Strada</th>
...
</tr>
<tr th:each="row : ${list}">
<td><a th:href="@{|/adresa/${row.id}|}">Edit</a></td>
<td th:text="${row.strada}" ></td>
....
</tr>
</table>
caller (index file):
...
<div th:replace="adresa::form(${adresa})"></div>
<div th:insert="adresa::table(${adresa.adresaList})"></div>
...
java controller:
...
@GetMapping(value="/adresa")
public String newAdresa(AdresaModel adresa, Model model) {
adresa.setAdresaList(adresaService.list());
model.addAttribute("adresa", adresa);
return "index";
}
@GetMapping(value="/adresa/{id}")
public String editAdresa(Model model, @PathVariable("id") Long id) {
AdresaModel adresa = adresaService.load(id);
adresa.setAdresaList(adresaService.list());
model.addAttribute("adresa", adresa);
return "index";
}
@PostMapping(value="/adresa")
public String save(AdresaModel adresaModel) {
Adresa adresa = buildDomain(adresaModel);
adresaService.save(adresa);
....
}
...
AdresaModel bean:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AdresaModel {
private Long id;
private String localitate;
private String sublocalitate;
private Long tara;
private Long judet;
private Long tipStrada;
private String strada;
private String numar;
private String bloc;
private String scara;
private String etaj;
private String apartament;
private String codPostal;
private List adresaList;
private List judetList;
private List tipStradaList;
private List taraList;
...
Thank you, hope to hear from you soon!
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (1 by maintainers)
Top GitHub Comments
I’ll have to dig into the attoparser code (Thymeleaf dependency that’s used for parsing templates) or run a few test cases to see what will happen if the fragment and HTML element names are the same, but from the problems shown in this issue it looks like the HTML element wins out.
Re: docs - the impression I get from reading the attoparser docs and the section in the Thymeleaf docs about the selector syntax (https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-c-markup-selector-syntax) is that both will be included:
This is an area I often forget about as it’s in a dependency, but I often end up advising developers to avoid fragment names that clash with existing HTML element names because of this unknown behaviour.
So the question here is: @ultraq what happens when the name of a fragment equals the name of a HTML tag? Will the tag have higher priority and prevent fragment parameters from being added? If so, should this be highlighted in the docs?