Art*_*hur 7 java spring thymeleaf
我的模板看不到从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);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器的代码:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
主模板的代码:
<!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>
Run Code Online (Sandbox Code Playgroud)
片段代码:
<!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>
Run Code Online (Sandbox Code Playgroud)
结果我看到方法调用的代码,比如
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SOME TITLE</title>
${CSSProcessor.setDebugCaller("Public")}
${CSSProcessor.setSiteRegion("public")}
${CSSProcessor.addCSS("/css/main.css")}
Run Code Online (Sandbox Code Playgroud)
为什么百里香叶没有调用方法,而是在输出页面打印这个文本?在例子中来自http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html方法调用具有相同的语法,如
${person.createCompleteName()}
Run Code Online (Sandbox Code Playgroud)
相同的代码适用于JSP,但不适用于百万美元.
小智 14
这可以通过两种方式在Thymeleaf中完成:
首先是使用特殊的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>
Run Code Online (Sandbox Code Playgroud)
第二种方式是:
<head th:fragment="publicSiteHeader" th:inline="text">
<title>SOME TITLE</title>
[["${CSSProcessor.setDebugCaller("Public")}"]]
[["${CSSProcessor.setSiteRegion("public")}"]]
[["${CSSProcessor.addCSS("/css/main.css")}"]]
</head>
Run Code Online (Sandbox Code Playgroud)
对于自然模板处理,第二选项是更优选的.有关内联的更多信息,请访问:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining
您可以通过 thymeleaf 调用方法,但这不是一个好的做法。thymeleaf 的理念与 JSP 不同 - 它尝试使用有效的 HTML 模板。老实说:在 JSP 中调用方法也不是好的做法。但我不是你的判断者,所以要调用使用不可见的 span 或 div 的方法,请尝试以下操作:
<span th:text="${myvariable.myfunct()}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35112 次 |
| 最近记录: |