代码结构:

1、ThymeleafController

  1. package com.xxx.firstboot.web;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9.  
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12.  
  13. @Api("测试Thymeleaf和devtools")
  14. @Controller
  15. @RequestMapping("/thymeleaf")
  16. public class ThymeleafController {
  17.  
  18. @ApiOperation("第一个thymeleaf程序")
  19. @RequestMapping(value = "/greeting", method = RequestMethod.GET)
  20. public String greeting(@RequestParam(name = "name", required = false, defaultValue = "world") String name,
  21. Model model) {
  22. model.addAttribute("xname", name);
  23. return "index";
  24. }
  25.  
  26. @ApiOperation("thymeleaf ajax")
  27. @ResponseBody
  28. @RequestMapping(value = "/ajax", method = RequestMethod.GET)
  29. public String ajax(@RequestParam("username") String username) {
  30. return username;
  31. }
  32.  
  33. }

说明:

  • 第一个是springMVC经典返回形式modelAndView
  • 第二个是ajax返回形式

2、index.html

  1. <!DOCTYPE HTML>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>index</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.  
  7. <link th:href="@{/bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
  8. <link th:href="@{/bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet" />
  9. </head>
  10. <body>
  11. <div class="panel panel-primary">
  12. <div class="panel-heading">hello</div>
  13. <div class="panel-body" th:text="${xname}"></div>
  14. </div>
  15. <div class="panel panel-warning">
  16. <div class="panel-heading">hello</div>
  17. <div id="usernamediv" class="panel-body"></div>
  18. </div>
  19. <script type="text/javascript" th:src="@{/js/jquery-1.11.1.js}"></script>
  20. <script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
  21. <script th:inline="javascript">
  22. $(function(){
  23. $.ajax({
  24. url:"/thymeleaf/ajax",
  25. data:{
  26. username:"xxx"
  27. },
  28. type:"get",
  29. dataType:"text",
  30. success:function(text){
  31. alert(text);
  32. $("#usernamediv").text(text);
  33. }
  34. });
  35. });
  36. </script>
  37. </body>
  38. </html>

说明:

  • 引入外界静态资源的方式@{/xxx},默认的静态资源的根是"static"
  • ajax的返回类型dataType要选好(一般就是"text"/"json")
  • ajax的请求方法类型type要与controller相同,否则抛出405错误

文档:

【第十八章】 springboot + thymeleaf的更多相关文章

  1. 第十八章 springboot + thymeleaf

    代码结构: 1.ThymeleafController package com.xxx.firstboot.web; import org.springframework.stereotype.Con ...

  2. 第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)

    brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...

  3. 第三十八章 springboot+docker(maven)

    回顾上一章的整个部署过程: 使用"mvn install"进行打包jar 将jar移动到与Dockerfile文件相同的文件夹下 编写Dockerfile文件 使用"do ...

  4. 不要再学 JSP 了,学 SpringBoot + Thymeleaf + Vue吧

    老读者就请肆无忌惮地点赞吧,微信搜索[沉默王二]关注这个在九朝古都洛阳苟且偷生的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题. 读 ...

  5. 《Linux内核设计与实现》读书笔记 第十八章 调试

    第十八章调试 18.1 准备开始          需要准备的东西: l  一个bug:大部分bug通常都不是行为可靠而且定义明确的 l  一个藏匿bug的内核版本:找出bug首先出现的版本 l  相 ...

  6. Python之路【第十八章】:Django基础

    Django基本配置 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Se ...

  7. 《Linux内核设计与实现》课本第十八章自学笔记——20135203齐岳

    <Linux内核设计与实现>课本第十八章自学笔记 By20135203齐岳 通过打印来调试 printk()是内核提供的格式化打印函数,除了和C库提供的printf()函数功能相同外还有一 ...

  8. 《Linux命令行与shell脚本编程大全》 第十八章 学习笔记

    第十八章:初识sed和gawk 文本处理 sed编辑器 sed编辑器可以基于输入到命令行的或是存储在命令文本文件中的命令来处理数据流中的数据. 它每次读取一行,用提供的编辑器命令匹配数据.按命令中指定 ...

  9. org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type

    前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题. 至于为什么已经写了一篇文章thymeleaf模板引擎调用java类中的方法,又多此一举的单独整 ...

  10. 第十八章 DjangoWeb开发框架

    第十八章 DjangoWeb开发框架 第一课 内容概要: 1.JS正则 -登录注册验证 2.组件 1.BootStrap -css -js 学习BootStrap规则 2.jQueryUI -css ...

随机推荐

  1. 003-spring cloud gateway-概述、Route模型、网关初始化配置过程、基本原理

    一.概述 网关服务核心是将进入的请求正确合理的路由到下层具体的服务进行业务处理,由此可见网关服务的核心就是路由信息的构建. Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到 ...

  2. [py]django上线部署-uwsgi+nginx+py3/django1.10

    https://github.com/lannyMa/django-uwsgi-nginx.git 单机调试启动-确保项目代码没问题 - 克隆代码进入项目 git clone https://gith ...

  3. jconsole监控远程 spring boot程序

    监控java 程序 增加启动参数 java  \ -Djava.rmi.server.hostname=192.168.1.97 \ -Dcom.sun.management.jmxremote \- ...

  4. (转)Elasticsearch查询规则------match和term

    es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询 ...

  5. Andrew Ng-ML-第十四章-无监督学习

    1.无监督学习概述  图1.无监督学习 有监督学习中,数据是有标签的,而无监督学习中的训练集是没有标签的,比如聚类算法. 2.k-means算法 k-means算法应用是十分广泛的聚类方法,它包括两个 ...

  6. Linux文本编辑器快捷方式

  7. iptables 常用命令

    iptables service iptables save \\保存 iptables -F \\清空所有规则 iptables -F -t nat \\清空nat表 iptables -t nat ...

  8. 集成树模型使用自动搜索模块GridSearchCV,stacking

    一. GridSearchCV参数介绍 导入模块: from sklearn.model_selection import GridSearchCV GridSearchCV 称为网格搜索交叉验证调参 ...

  9. testng入门教程16数据驱动(把数据写在xml)

    testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...

  10. 用python与文件进行交互

    一.文件处理 1.介绍 计算机系统:计算机硬件,操作系统,应用程序 应用程序无法直接操作硬件,通过操作系统来操作文件,进而读/写硬件中的文件. python打开文件过程: #打开 f=open('a. ...