복붙노트

[SPRING] Thymeleaf에서 객체의 메서드를 호출하는 방법?

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

왜 thymeleaf는 메소드를 호출하지 않았지만 출력 페이지에서이 텍스트를 인쇄합니까? 예를 들어 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html에서 메소드 호출은 다음과 같은 구문을 사용합니다.

${person.createCompleteName()}

같은 코드가 JSP에서는 잘 작동하지만 thymeleaf에서는 작동하지 않습니다.

해결법

  1. ==============================

    1.Thymeleaf에서는 두 가지 방법으로이를 수행 할 수 있습니다.

    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

  2. ==============================

    2.thymeleaf를 통해 메소드를 호출 할 수는 있지만 좋은 방법은 아닙니다. thymeleaf는 JSP와는 다른 철학을 가지고 있습니다 - 유효한 HTML tamplate를 사용하려고합니다. 그리고 JSP로 메소드를 호출하는 것이 가장 좋은 방법이기도합니다. 그러나 나는 당신의 판사가 아니므로 메소드를 비 가시 범위 또는 div로 사용하려면 다음과 같이 시도하십시오.

    thymeleaf를 통해 메소드를 호출 할 수는 있지만 좋은 방법은 아닙니다. thymeleaf는 JSP와는 다른 철학을 가지고 있습니다 - 유효한 HTML tamplate를 사용하려고합니다. 그리고 JSP로 메소드를 호출하는 것이 가장 좋은 방법이기도합니다. 그러나 나는 당신의 판사가 아니므로 메소드를 비 가시 범위 또는 div로 사용하려면 다음과 같이 시도하십시오.

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

    3.Thymeleaf는 JSP처럼 작동하지 않습니다. 그것은 "th :"라는 접두사가 붙은 새로운 속성으로 기존 HTML 요소를 확장함으로써 작동합니다. 그리고 이러한 추가 속성에서만 변수를 참조 할 수 있으므로 (따라서 메소드에서 메소드를 호출 할 수 있습니다).

    Thymeleaf는 JSP처럼 작동하지 않습니다. 그것은 "th :"라는 접두사가 붙은 새로운 속성으로 기존 HTML 요소를 확장함으로써 작동합니다. 그리고 이러한 추가 속성에서만 변수를 참조 할 수 있으므로 (따라서 메소드에서 메소드를 호출 할 수 있습니다).

    예 :

    는 thymeleaf와 함께 작동합니다.

    그러나

    $ {contentOfTheParagraph} "는 그렇지 않습니다.

  4. from https://stackoverflow.com/questions/28067861/how-to-call-objects-method-from-thymeleaf by cc-by-sa and MIT license