Sumit
Sumit

Reputation: 1669

Invoke and load Spring MVC controller request method from Thymeleaf

I am using Spring Boot 1.5.4 (Spring MVC) and Thymeleaf 3.0.6 (Layout Dialect 2.2.2)

Assume that I am rendering the model from a Spring MVC Controller method with a Thymeleaf template (template A), which outputs HTML A.

Is it possible, from within template A, to call/invoke another Spring MVC controller method (which then will render the model with a different thymeleaf template) outputting HTML B, and load the rendered results into HTML A?

Something like what the struts2 action tag does with the executeResult=true Param. https://www.tutorialspoint.com/struts_2/struts_action_tag.htm

I've looked into thymeleaf include and replace, but they only seem to work with loading the html fragment not an entire Spring MVC Request

Upvotes: 2

Views: 632

Answers (1)

juanlumn
juanlumn

Reputation: 7095

You can try to make an AJAX request to the Spring MVC controller method which will return the template B.

Then, once you have the response you can set the response as an existing html element in your DOM, something like:

$.get("your_end_point", function(data, status){  
 document.getElementById("your_template_B_Container_DIV_ID").appendChild(data);
});

This is only a guess, also keep on mind that if you create a template with body, head... this will probably not work, try to use a template with no body or head tags. For example you can try with a template made only with div tags ans see if you can render it.

Upvotes: 0

Related Questions