如何从Thymeleaf调用对象的方法? [英] How to call object's method from Thymeleaf?

查看:3440
本文介绍了如何从Thymeleaf调用对象的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模板看不到从Spring传递的对象。

My template do not see objects, passed from 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的代码:

Contoller's code:

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

结果我看到方法调用的代码,如

As result I see code of the method calling, like

<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 方法调用具有相同的语法,例如

Why thymeleaf didn't call methods, but print this text at the output page? In example from http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html method calling has same syntax, like

${person.createCompleteName()}

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

The same code works good with JSP, but do not work with thymeleaf.

推荐答案

这可以通过两种方式在Thymeleaf中完成:

That can be done in Thymeleaf in two ways:

首先是使用特殊的Thymeleaf:

First is to use special for 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 #inline

For natural template processing second option is more preferable. More info about inlining can be found here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining

这篇关于如何从Thymeleaf调用对象的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆