Invoke and load Spring MVC controller request method from Thymeleaf - spring

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

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.

Related

Is there a way to retrieve data from redis in my jsp pages?

I am creating a spring boot server, and I would like the initial page (index.jsp) to be a datatable with information coming from the redis database. Anyone know how to do this with javascript or inside jsp pages?
First of all JSPs are outdated technology so I recommend you to go for thymeleaf which is a perfect choice with spring boot instead of JSP.
JAVA approach :
Just create a mapping with / base url and fetch Redis data using normal approach and use it in UI.
GetMapping("/")
public String index(){
//Fetch Redis data here add into model attributes
return "index";
}
JavaScript/inside JSP approach :
You can create a mapping method with some url like above and make an AJAX call on page load from the JSP page and get the JSON format data and use it.
Don't forget to put #ResponseBody annotation on top of the method which will return the data(JSON) instead of the page.
Refer call ajax to spring controller

How to create a Controller to render a custom Error Template in Spring Boot

I'm using Spring Boot and FreeMarker.
To render the header I need to put a dynamically created object in the model. Same to render the footer. I have a page.ftl that every other template includes so that every page have a header and a footer.
I want to render an 404 page which also shows this header and footer, so ideally the view of this page should be a free marker template.
I notice that if I have an error.ftl, Spring Boot would use it in case of an error, but where can I put logic that adds the header and footer so that my error page looks nice?
The answer was to create a #ControllerAdvice
Inside there is one method annotated with #ResponseStatus(HttpStatus.NOT_FOUND) and with #ExceptionHandler for the project's own PageNotFoundException. That method returns a ModelAndView object where I can put the name of whatever view I want to render (doesn't matter if it's FreeMarker or not). Inside the method I construct the header and footer.
The same can be done for a 500 page.

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

How to use annotated Spring controllers with Freemarker to return custom ajax respones

I primarily use annotated controllers that return ModelAndView object used by Spring and Freemarker to render and return pages to browsers - works great. I'd now like to respond to ajax gets and posts that return html fragments as payload inside an XML message.
The problem I'm having not returning a ModelAndView objects seems to confuse Spring WRT to #ModelAttribute (s) and session objects that support data to / and from forms.
I've had to hack the freemarker template to support i18n messages, etc. I'm finding that simply rendering an FTL file while also trying to utilize Spring's object rendering from froms is becoming quite a rabbit hole.
I'm trying to manually (guess what and) do the things that get done behind the scenes and it's not working optimally. To edit an object I'm manually placing the object in the session on rendering the edit form. When the post comes back there are residual/different values in the object I get back out of the session AND the ModelAttribute that Spring rendered for me.
Bottom line? Questions about Spring, Freemarker and custom non-ModelAndView responses that are rendered by Freemarker.
Add another instance of FreeMarkerViewResolver that only that has viewNames ='*.frg'. The viewClass should be your own class that renders the template the way you want by overriding the processTemplate method.

If you use a regular HTML <form> tag instead of the Spring <form:form> tag, how do you specify where it's submitted?

In the Spring docs, the section on multipart support has an example of specifying a multipart form as this:
<form method="post" action="upload.form" enctype="multipart/form-data">
This doesn't use the Spring form tags which I am familiar with and instead submits the form to the upload.form action.
How does Spring know what upload.form means? Do you have to create a mapping for it and, if so, where is this done?
upload.form is the URL of the HTTP POST request that is created by this form. It will be relative to the URL of the view that generated the form in the first place, so if the form shown above is rendered by /mysite/myapp/myform.html, it will submit to /mysite/myapp/upload.form
It then becomes a matter of configuring Spring to handle "/upload.form" request accordingly, such as through #RequestMapping if you're using annotation-based Spring MVC. See here for more information.

Resources