thymeleaf template engine calls methods in java classes (with source code)

foreword

"Docker+SpringBoot+Mybatis+thymeleaf's Java blog system is open source"

Due to the open source project, many friends who have used the My Blog project will contact me to solve problems. Add my qq directly to talk to me about the problems you have encountered. Some problems are relatively simple and straightforward to solve, and the solution records of some problems are also kept in the issue records. Some of them have related tutorials on the Internet. If time permits, I will also do a separate blog to introduce and answer.

The sample code address in this article is at: springboot-thymeleaf , I hope you can support it.

problem analysis

I received this question on the issue page of the My Blog project. The issue describes how to call the method in the java class in the thymeleaf template page. The problem description is as follows:

thymeleaf issue

But there are only screenshots, there is no valid information such as the reason for the error and the screenshot of the error, and the buddy did not leave any other information, so he can only simulate and try to solve and analyze the problem by himself, which can be obtained from the description of the issue. Exception information:

Attempted to call method test() on null context object

An answer can be roughly drawn from this exception information. The called test() method is in an empty object, that is, there is no corresponding java instance in the context field, so it cannot be called.

I also responded to this issue on the same day, and the answers given are as follows:

The context of thymeleaf, that is, the place where data is provided, the web-based context, that is, the WebContext adds param, session, and application variables to the context, and automatically adds request attributes to the context variable map, which can be accessed directly in the template.

Although the answer was given, it was more theoretical, and the bug was not reproduced, so I rebuilt a springboot+thymeleaf project according to the description in the issue, and tried to reproduce the problem and solve it.

Problem reproduces

The code was rewritten, and the called java instance was not put into the request object. The reproduced code is as follows:

public class MethodTestController {

    @RequestMapping("/test1")
    public String test1(HttpServletRequest request) {
        return "test";
    }
}    

Finally reproduced this issue:
null object

After reproducing this problem, I thought, what error will be reported if the called method is empty? Try to write:

<p th:text="'调用不存在的方法: ' + ${MethodTest.service()} + '!'"/>

Got the following error:
null method

The two exceptions are:

  • Exception 1:
Attempted to call method on null context object

调用的方法处于一个空对象中,即调用实例为空。
  • Exception 2:
Method cannot be found

方法不存在

Both exceptions are org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expressionexceptions.

problem solution

Analysis so far, the problem of abnormality has been very clear:

  • For exception 1, it is necessary to store the java instance expected to be called into the context field of thymeleaf. The code level is: store the instance object into the Request object.
  • For exception 2, it is simpler, check the code carefully, don't write the wrong method name, and don't use undefined methods.

Epilogue

First published on my personal blog .

If you have any questions or have some good ideas, please leave me a message, and thank you for pointing out the problems in the project.

The code and this problem are all in the My Blog project. If you want to continue to understand the project, you can check the entire series of articles Java open source blog My-Blog (SpringBoot+Docker) series articles , you can also go to my GitHub repository or open source China View the source code and detailed deployment process and usage documentation in the code repository .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774899&siteId=291194637