How to call a method of an object from Timeleaf?

My template does not see objects passed from Spring.

My code is:

public class PublicModelAndView extends ModelAndView { @Autowired TemplateModulesHandler templateModulesHandler; public void init() { setViewName("index"); CSSProcessor cSSProcessor = new CSSProcessor(); cSSProcessor.setSiteRegion("public"); super.addObject("CSSProcessor", cSSProcessor); JSProcessor jSProcessor = new JSProcessor(); super.addObject("JSProcessor", jSProcessor); templateModulesHandler.setPublicModelAndView(this); } } 

Contoller Code:

 @SpringBootApplication @Controller public class IndexPage { @Autowired PublicModelAndView publicModelAndView; @Autowired OurServicesBean ourServicesBean; @Autowired PortfolioBean portfolioBean; @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView indexPage() { publicModelAndView.setTemplate("publicSiteIndexPage"); publicModelAndView.addObject("ourServices", ourServicesBean.getMenu()); publicModelAndView.addObject("portfolioWorkTypes", portfolioBean.getWorkTypes()); publicModelAndView.addObject("portfolioWorks", portfolioBean.getWorks()); return publicModelAndView; } } 

The main template code:

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" > <head th:include="headerAndFooter/fragments/header :: publicSiteHeader"> <title></title> </head> <body> hello! </body> </html> 

Fragment Code:

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head th:fragment="publicSiteHeader"> <title>SOME TITLE</title> ${CSSProcessor.setDebugCaller("Public")} ${CSSProcessor.setSiteRegion("public")} ${CSSProcessor.addCSS("/css/main.css")} </head> <body> </body> </html> 

As a result, I see the method call code, for example

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SOME TITLE</title> ${CSSProcessor.setDebugCaller("Public")} ${CSSProcessor.setSiteRegion("public")} ${CSSProcessor.addCSS("/css/main.css")} 

Why didn’t thimeleaf invoke methods, but print this text on the output page? In the example from http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html, the invocation method has the same syntax as

 ${person.createCompleteName()} 

The same code works well with JSP, but does not work with thimeleaf.

+6
source share
3 answers

There are two ways to do this in Timeleaf:

First you need to use the special one for Thymeleaf:

 <head th:fragment="publicSiteHeader"> <title>SOME TITLE</title> <th:block th:text="${CSSProcessor.setDebugCaller("Public")}"/> <th:block th:text="${CSSProcessor.setSiteRegion("public")}"/> <th:block th:text="${CSSProcessor.addCSS("/css/main.css")}"/> </head> 

And the second way:

 <head th:fragment="publicSiteHeader" th:inline="text"> <title>SOME TITLE</title> [["${CSSProcessor.setDebugCaller("Public")}"]] [["${CSSProcessor.setSiteRegion("public")}"]] [["${CSSProcessor.addCSS("/css/main.css")}"]] </head> 

For natural processing of the template, the second option is preferable. More information on inlining can be found here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining

+8
source

You can call methods through thimeleaf, but this is not a good practice. Timelear has a different philosophy than JSP - it tries to use valid HTML templates. And being the most interesting to call methods in JSP is also not good practice. But I'm not your judge, so to invoke a method, use an invisible range or div, try something like:

 <span th:text="${myvariable.myfunct()}" /> 
+3
source

Thimeleaf does not work as a JSP. It works by expanding existing HTML elements with new attributes prefixed with "th:". And you can refer to variables (and therefore, call them by them), only in additional attributes.

eg. <p th:text="${contentOfTheParagraph}" /> will work with the thimeleaf

But <p>${contentOfTheParagraph}"</p> will not.

0
source

Source: https://habr.com/ru/post/981345/


All Articles