后台的朋友有推荐这个插件。然后就是使用了下 各种报错,后台数据一直传不到前台,查了下原因,。。就是 注解问题
首先看错误代码:
@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>这就是全部内容了 继续踩坑学!