如何从 Thymeleaf 调用对象的方法?

How to call object's method from 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);

    }

}

控制器代码:

@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。

Thymeleaf 不像 JSP 那样有效。它通过使用前缀为 "th:" 的新属性扩展现有 HTML 元素来工作。并且您只能在这些额外属性中引用变量(并因此调用它们的方法)。

例如<p th:text="${contentOfTheParagraph}" /> 将与 thymeleaf 一起使用

但是<p>${contentOfTheParagraph}"</p>不会。

您可以通过 thymeleaf 调用方法,但这不是一个好习惯。 thymeleaf 的理念与 JSP 不同——它尝试使用有效的 HTML 模板。老实说:在 JSP 中调用方法也不是好的做法。但我不是你的判断者,所以调用一个方法使用不可见的跨度或 div,尝试类似的东西:

<span th:text="${myvariable.myfunct()}" />

这可以在 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>

第二种方式是:

<head th:fragment="publicSiteHeader" th:inline="text">

    <title>SOME TITLE</title>

     [["${CSSProcessor.setDebugCaller("Public")}"]]
     [["${CSSProcessor.setSiteRegion("public")}"]]
     [["${CSSProcessor.addCSS("/css/main.css")}"]]
</head>

对于自然模板处理,第二个选项更可取。可以在此处找到有关内联的更多信息:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining