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.