【问题标题】:How to call object's method from Thymeleaf?如何从 Thymeleaf 调用对象的方法?
【发布时间】:2015-03-20 00:14:59
【问题描述】:

我的模板看不到对象,从 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。

【问题讨论】:

    标签: java spring thymeleaf


    【解决方案1】:

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

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

    【讨论】:

    • 很高兴知道为什么这不是一个好习惯;并且您建议的方法似乎不起作用。
    【解决方案2】:

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

    第二种方式是:

    <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

    【讨论】:

      【解决方案3】:

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

      例如&lt;p th:text="${contentOfTheParagraph}" /&gt; 将与百里香一起使用

      但&lt;p&gt;${contentOfTheParagraph}"&lt;/p&gt; 不会。

      【讨论】:

      • 这不是真的。您可以使用 th:block 或 th:inline 执行

        [[${contentOfTheParagraph}"]]

        或

        ${contentOfTheParagraph}"

        。在下面检查我的答案。
      猜你喜欢
      • 2014-06-26
      • 2016-02-11
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 2022-11-30
      • 1970-01-01
      相关资源
      最近更新 更多