Thymeleaf forms cannot be processed in EngineContext

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Thymeleaf forms cannot be processed in EngineContext

Tom Bombadil
I am creating a web application in java using the Spring framework with thymeleaf templates.

I am trying to manually render an HTML template, outside of the web request context and return it serialized as an AJAX response.

This does not work when using a form, It throws an error when using th:field in the thymeleaf template.

I first posted this on StackOverflow
https://stackoverflow.com/questions/53761744/spring-templateengine-process-error-on-thfield

Then procceded to try to register it as a bug in Spring
https://jira.spring.io/browse/SPR-17603


After a little chat with the people at Spring i have now been redirected here as the problem is in the Thymeleaf part of the Spring java code. The links above contains all of the details but the short story is:

Any html template with th:field included and rendered through TemplateEngine .process will be using EngineContext to render it (not WebEngineContext), EngineContext will never create the nescessary SpringWebMvcThymeleafRequestContext for the field bindings to work in a form.

I found a "dirty fix" by creating my own SpringWebMvcThymeleafRequestContext and injected it in to make it accessible for EngineContext.

I am asking for an official fix to this.
Make it possible to use thymeleaf templates running through Spring TemplateEngine .process(..)

A possible solution would be to simply create an object of SpringWebMvcThymeleafRequestContext in EngineContext and use it to parse forms.
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf forms cannot be processed in EngineContext

Reiju
1. Controller (return ur thymeleaf fragment as simple string-view, pass data to Model)
@GetMapping("/test/form")
public String getForm(Model model) {
        model.addAttribute("customer", new Customer());
        return "formFragments :: content";
}

2. th:fragment (create separate file formFragments.html with ur th:fragments)
<div th:fragment="content" xmlns:th="http://www.thymeleaf.org">

    <form method="post" th:action="${'/customer/add'}" th:object="${customer}">
        <label>Name</label>
        <input th:field="*{name}" type="text"/>
    </form>

</div>


3. HTML + JS (insert your form as innerHTML)
<div id="container"></div>
<button id="btn" type="button">Get form</button>
<script>
    $(document).ready(function () {
         $('#btn').on('click', function () {
             $.ajax({
                 url: '/test/form',
                 type: 'get',
                 success: function (data) {
                     $('#container').html(data);
                 }
             });
         })
    });
</script>

Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf forms cannot be processed in EngineContext

Tom Bombadil
How would i apply this to my sample use case?

I want to return this in json format with multiple values.
{
    "form_html": "<form> ..... </form>",
    "additional_data": "abc123_validate_test..",
    ...
}

Your proposed solution returns the name of the template fragment, i don't see any way to manipulate the return value after this.