question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem inserting a form from a fragment

See original GitHub issue

I’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:open
  • Created 3 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ultraqcommented, Oct 16, 2020

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:

x is exactly equivalent to //x (search an element with name or reference x at any depth level, a reference being a th:ref or a th:fragment attribute).

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.

0reactions
xtianuscommented, Oct 15, 2020

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Contact form on fragment not working - Stack Overflow
I'm trying to create a simple feedback form on my fragment, but the app stops when I ... Can anyone please tell me...
Read more >
Creating and Using Fragments | CodePath Android Cliffnotes
Overview. A fragment is a reusable class implementing a portion of an activity. A Fragment typically defines a part of a user interface....
Read more >
AEM Forms Tutorial - Creating and Using Form Fragments
Sign up for a free daily demo! These occur M-F and cover a wide variety of topics.
Read more >
Create a fragment - Android Developers
Fragments require a dependency on the AndroidX Fragment library. You need to add the Google Maven repository to your project's settings.gradle file in...
Read more >
Rendering Forms Based on Fragments - Experience League
When you need to use some of the same content in multiple forms, it is faster and simpler to use a fragment than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found