Reputation: 3473
My template do not see objects, passed from Spring.
My code:
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's 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;
}
}
Main template's 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's 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 result I see code of the method calling, like
<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 thymeleaf didn't call methods, but print this text at the output page? In example from http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html method calling has same syntax, like
${person.createCompleteName()}
The same code works good with JSP, but do not work with thymeleaf.
Upvotes: 16
Views: 70822
Reputation: 1661
You can call methods via thymeleaf but it is not a good practice. The thymeleaf has different philosophy than JSP - it tries to use valid HTML templates. And to be honest: Calling methods in JSP is not good practice as well. But I am not your judge, so to call a method use non visible span or div, try something like:
<span th:text="${myvariable.myfunct()}" />
Upvotes: 5
Reputation: 782
That can be done in Thymeleaf in two ways:
First is to use special 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 is:
<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 template processing second option is more preferable. More info about inlining can be found here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining
Upvotes: 22
Reputation: 675
Thymeleaf does not work like JSP. It works by extending existing HTML elements with new attributes prefixed by "th:". And you can reference variables (and therefore call method on them) only in theses extra-attributes.
E.g. <p th:text="${contentOfTheParagraph}" />
will work with thymeleaf
But <p>${contentOfTheParagraph}"</p>
will not.
Upvotes: 1