thymeleaf踩坑系列1

    技术2022-07-10  111

    thymeleaf踩坑1

    准备学后台了,前端不太好混 可能是我菜吧。。 今天第一次用thymeleaf这个插件,就是吧后台的数据通过这个插件传递到前端的h5界面上显示数据。。。。

    后台的朋友有推荐这个插件。然后就是使用了下 各种报错,后台数据一直传不到前台,查了下原因,。。就是 注解问题

    首先看错误代码:

    @RestController public class SampleController { @RequestMapping("/test") public String thymeleaf(Model model) { model.addAttribute("name", "你猜猜看显示不显示"); return "hello"; } }

    在这里看本身是没什么问题,RestController 这个注解是集成了

    @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { @AliasFor( annotation = Controller.class ) String value() default ""; }

    在这里我们可以看到里面是有集成Controller 和ResponseBody 的,我以为这就可以直接显示在前端数据了,毕竟 RestController 包含了Controller 。。。 没想到的是 在thymeleaf中 是不可以使用RestController的 ,要使用 Controller 这个注解。、、——————所以 修改注解 ,正确代码是

    @Controller public class SampleController { @RequestMapping("/test") public String thymeleaf(Model model) { model.addAttribute("name", "你猜猜显示不显示"); return "hello"; } }

    然后就显示了。。。我好尴尬。。哦 我发下前端h5页面的代码。。其实超简单 就一行代码。。

    <!DOCTYPE HTML> <html lang="en" xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>hellomode</title> </head> <body> <h1 th:text="${name}"></h1> </body> </html>

    这就是全部内容了 继续踩坑学!

    Processed: 0.009, SQL: 9