Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call object's method from Thymeleaf?

My template do not see objects, passed from 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? In example from http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html method calling has same syntax, like

${person.createCompleteName()}

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

like image 796
Arthur Avatar asked Jan 21 '15 12:01

Arthur


People also ask

How do I use the Thymeleaf th object?

We use th:action to provide the form action URL and th:object to specify an object to which the submitted form data will be bound. Individual fields are mapped using the th:field=”*{name}” attribute, where the name is the matching property of the object.

How do you access variables in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages .

How do you pass parameters in Thymeleaf?

The attribute th:name=”participant” binds the value of the input field to the parameter participant of the controller. For this to work, we need to annotate the parameter with @RequestParam(value = “participant”).

What is #{} in Thymeleaf?

#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.


1 Answers

That can be done in Thymeleaf in two ways:

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>

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

like image 95
Ostap Gonchar Avatar answered Oct 09 '22 01:10

Ostap Gonchar