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.
We use th:action to provide the form action URL and th:object to specify an object to which the submitted form data will be bound. Individual fields are mapped using the th:field=”*{name}” attribute, where the name is the matching property of the object.
In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages .
The attribute th:name=”participant” binds the value of the input field to the parameter participant of the controller. For this to work, we need to annotate the parameter with @RequestParam(value = “participant”).
#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With