【发布时间】:2015-03-20 00:14:59
【问题描述】:
我的模板看不到对象,从 Spring 传递过来。
我的代码:
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);
}
}
控制器代码:
@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;
}
}
主模板代码:
<!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>
片段的代码:
<!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>
结果我看到了方法调用的代码,比如
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SOME TITLE</title>
${CSSProcessor.setDebugCaller("Public")}
${CSSProcessor.setSiteRegion("public")}
${CSSProcessor.addCSS("/css/main.css")}
为什么 thymeleaf 不调用方法,而是在输出页面打印此文本?在来自http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html 的示例中,方法调用具有相同的语法,例如
${person.createCompleteName()}
同样的代码适用于 JSP,但不适用于 thymeleaf。
【问题讨论】: