Thymeleaf 没有从 Spring 中看到 objects
Thymeleaf didn't see objects from Spring
我的模板没有看到 objects,从 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("pageTitle", "TITLE!!!!!!!!!!!!!!");
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>${pageTitle}</title>
<!--[if lte IE 8]>
<script src="<?= SITE_TEMPLATE_PATH ?>/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
</body>
</html>
因此我没有看到 object pageTitle 的值,但我在页面输出代码中看到
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>${pageTitle}</title>
为什么 thymeleaf 没有将 pageTitle 的值粘贴到标题标签的打开和关闭之间?
相同的代码适用于 JSP,但不适用于 thymeleaf。
Thymeleaf 不是 JSP,所以这就是您的模板无法按预期工作的原因。
看这里 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-texts 并使用类似的东西:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<title th:text="#{pageTitle}">page title</title>
已编辑 - 我的解决方案是针对本地化文本,这无论如何都是很好的做法。如果你想使用变量的内容而不是使用 $.
我的模板没有看到 objects,从 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("pageTitle", "TITLE!!!!!!!!!!!!!!");
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>${pageTitle}</title>
<!--[if lte IE 8]>
<script src="<?= SITE_TEMPLATE_PATH ?>/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
</body>
</html>
因此我没有看到 object pageTitle 的值,但我在页面输出代码中看到
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>${pageTitle}</title>
为什么 thymeleaf 没有将 pageTitle 的值粘贴到标题标签的打开和关闭之间?
相同的代码适用于 JSP,但不适用于 thymeleaf。
Thymeleaf 不是 JSP,所以这就是您的模板无法按预期工作的原因。
看这里 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-texts 并使用类似的东西:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<title th:text="#{pageTitle}">page title</title>
已编辑 - 我的解决方案是针对本地化文本,这无论如何都是很好的做法。如果你想使用变量的内容而不是使用 $.