简体   繁体   English

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

[英]How to call object's method from Thymeleaf?

My template do not see objects, passed from Spring.我的模板看不到从 Spring 传递过来的对象。

My code:我的代码:

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'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;

    }

}

Main template's code:主模板代码:

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

Fragment's code:片段代码:

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

Why thymeleaf didn't call methods, but print this text at the output page?为什么 thymeleaf 不调用方法,而是在输出页面打印此文本? In example from http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html method calling has same syntax, likehttp://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html 的示例中,方法调用具有相同的语法,例如

${person.createCompleteName()}

The same code works good with JSP, but do not work with thymeleaf.相同的代码适用于 JSP,但不适用于 thymeleaf。

That can be done in Thymeleaf in two ways:这可以通过两种方式在 Thymeleaf 中完成:

First is to use special for Thymeleaf:首先是对 Thymeleaf 使用 special:

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

And the second way is:第二种方式是:

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

    <title>SOME TITLE</title>

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

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关于内联的更多信息可以在这里找到: http : //www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining

You can call methods via thymeleaf but it is not a good practice.您可以通过 thymeleaf 调用方法,但这不是一个好习惯。 The thymeleaf has different philosophy than JSP - it tries to use valid HTML templates. thymeleaf 与 JSP 有着不同的理念——它尝试使用有效的 HTML 模板。 And to be honest: Calling methods in JSP is not good practice as well.老实说:在 JSP 中调用方法也不是好的做法。 But I am not your judge, so to call a method use non visible span or div, try something like:但我不是您的判断者,因此要使用不可见的跨度或 div 调用方法,请尝试以下操作:

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

Thymeleaf does not work like JSP. Thymeleaf 不像 JSP 那样工作。 It works by extending existing HTML elements with new attributes prefixed by "th:".它的工作原理是使用以“th:”为前缀的新属性扩展现有的 HTML 元素。 And you can reference variables (and therefore call method on them) only in theses extra-attributes.并且您只能在这些额外属性中引用变量(并因此调用它们的方法)。

Eg <p th:text="${contentOfTheParagraph}" /> will work with thymeleaf例如<p th:text="${contentOfTheParagraph}" />将与百里香叶一起使用

But <p>${contentOfTheParagraph}"</p> will not.但是<p>${contentOfTheParagraph}"</p>不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM