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

时间:2015-01-21 12:59:36

标签: 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);

    }

}

Contoller的代码:

@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")}

为什么百里香叶没有调用方法,而是在输出页面打印此文本?在示例中,http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html方法调用具有相同的语法,如

${person.createCompleteName()}

相同的代码适用于JSP,但不适用于百里香。

3 个答案:

答案 0 :(得分:13)

可以通过两种方式在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

答案 1 :(得分:3)

你可以通过百里香调用方法,但这不是一个好习惯。百万美元与JSP有着不同的哲学 - 它试图使用有效的HTML模板。并且在JSP中调用方法是最好的也不是好的做法。但我不是你的判断,所以调用方法使用不可见的span或div,尝试类似:

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

答案 2 :(得分:0)

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

E.g。 <p th:text="${contentOfTheParagraph}" />将与百里香一起使用

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