Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17603

Error on thymeleaf th:field in TemplateEngine process

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Web
    • Labels:

      Description

      There was little response in StackOverflow but the nature of this problem leads me to believe that it is a bug and so i'm posting this here. I have looked up and down on the web without finding anything related specifically to this.

       

      I am also setting the priority to Major as it is putting some major problems into our development.

       

      I am currently porting an older web application to spring, The older web application had ajax requests where it would return some html code along with some status messages in json format.

       

      Trying to reimplement this functionality leads to error messages in spring.

       Error occurs when processing thymeleaf template containing th:field using TermplateEngine process method.

       

      Thymeleaf template
      <form th:object="${customer}">    
      <label>Name</label>
        <input type="text" th:field="*{name}" />
      </form> 

       

      Controller
      // — Class member —
      @Autowired
      private TemplateEngine templateEngine;
      // ----------

      @ResponseBody
      @GetMapping(value="/form1")
      public String form1() throws JsonProcessingException

      {     Customer customer = new Customer("Burger King");     Context templateContext = new Context(); templateContext.setVariable("customer", customer);     AjaxResponse response = new AjaxResponse(); response.html = templateEngine.process("form", templateContext); // <-- ERROR       response.additionalData = "ab123"; return objectMapper.writeValueAsString(response); }

       

      Error output
       org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/form.html]")

      Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Cannot process attribute '{th:field,data-th-field}': no associated BindStatus could be found for the intended form binding operations. This can be due to the lack of a proper management of the Spring RequestContext, which is usually done through the ThymeleafView or ThymeleafReactiveView (template: "form" - line 3, col 21)
       

      This error occurs only when using process method and will work as expected when returning "form" without @ResponseBody

       

      It also works when using th:value instead of th:field.

       

      And so to summarize this bug is causing errors when processing a thymeleaf template containing th:field using TemplateEngine .process method.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            Bombadil Tom
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 23 weeks, 5 days ago