【第十八章】 springboot + thymeleaf
代码结构:

1、ThymeleafController
package com.xxx.firstboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api("测试Thymeleaf和devtools")
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@ApiOperation("第一个thymeleaf程序")
@RequestMapping(value = "/greeting", method = RequestMethod.GET)
public String greeting(@RequestParam(name = "name", required = false, defaultValue = "world") String name,
Model model) {
model.addAttribute("xname", name);
return "index";
}
@ApiOperation("thymeleaf ajax")
@ResponseBody
@RequestMapping(value = "/ajax", method = RequestMethod.GET)
public String ajax(@RequestParam("username") String username) {
return username;
}
}
说明:
- 第一个是springMVC经典返回形式modelAndView
- 第二个是ajax返回形式
2、index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link th:href="@{/bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{/bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet" />
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">hello</div>
<div class="panel-body" th:text="${xname}"></div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">hello</div>
<div id="usernamediv" class="panel-body"></div>
</div>
<script type="text/javascript" th:src="@{/js/jquery-1.11.1.js}"></script>
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<script th:inline="javascript">
$(function(){
$.ajax({
url:"/thymeleaf/ajax",
data:{
username:"xxx"
},
type:"get",
dataType:"text",
success:function(text){
alert(text);
$("#usernamediv").text(text);
}
});
});
</script>
</body>
</html>
说明:
- 引入外界静态资源的方式@{/xxx},默认的静态资源的根是"static"
- ajax的返回类型dataType要选好(一般就是"text"/"json")
- ajax的请求方法类型type要与controller相同,否则抛出405错误
文档:
- thymeleaf的其他配置查看:《springboot实战》、http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html
- bootstrap:http://www.bootcss.com/
【第十八章】 springboot + thymeleaf的更多相关文章
- 第十八章 springboot + thymeleaf
代码结构: 1.ThymeleafController package com.xxx.firstboot.web; import org.springframework.stereotype.Con ...
- 第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)
brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...
- 第三十八章 springboot+docker(maven)
回顾上一章的整个部署过程: 使用"mvn install"进行打包jar 将jar移动到与Dockerfile文件相同的文件夹下 编写Dockerfile文件 使用"do ...
- 不要再学 JSP 了,学 SpringBoot + Thymeleaf + Vue吧
老读者就请肆无忌惮地点赞吧,微信搜索[沉默王二]关注这个在九朝古都洛阳苟且偷生的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题. 读 ...
- 《Linux内核设计与实现》读书笔记 第十八章 调试
第十八章调试 18.1 准备开始 需要准备的东西: l 一个bug:大部分bug通常都不是行为可靠而且定义明确的 l 一个藏匿bug的内核版本:找出bug首先出现的版本 l 相 ...
- Python之路【第十八章】:Django基础
Django基本配置 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Se ...
- 《Linux内核设计与实现》课本第十八章自学笔记——20135203齐岳
<Linux内核设计与实现>课本第十八章自学笔记 By20135203齐岳 通过打印来调试 printk()是内核提供的格式化打印函数,除了和C库提供的printf()函数功能相同外还有一 ...
- 《Linux命令行与shell脚本编程大全》 第十八章 学习笔记
第十八章:初识sed和gawk 文本处理 sed编辑器 sed编辑器可以基于输入到命令行的或是存储在命令文本文件中的命令来处理数据流中的数据. 它每次读取一行,用提供的编辑器命令匹配数据.按命令中指定 ...
- 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类中的方法,又多此一举的单独整 ...
- 第十八章 DjangoWeb开发框架
第十八章 DjangoWeb开发框架 第一课 内容概要: 1.JS正则 -登录注册验证 2.组件 1.BootStrap -css -js 学习BootStrap规则 2.jQueryUI -css ...
随机推荐
- 洛谷P1494小Z的袜子 [国家集训队] 莫队
正解:莫队 解题报告: 这是,传送门qwq 昂大概是莫队板子题? 首先可以推出来答案是(∑C(2,color[i]))/C(2,r-l+1)趴?挺显然的不解释了qwq 然后显然除数直接做就成,考虑怎么 ...
- 深入浅出 TCP/IP 协议栈
写的不错:http://www.cnblogs.com/onepixel/p/7092302.html#3899256
- sql server递归日期
在做项目任务时,需要将一个日期范围转换为日期表. 例如:日期范围(2017年01月21日~2017年02月20日).转换成一日为单位的日期表,如下. 2017-01-21 2017-01-22 201 ...
- mysql 数据操作 多表查询 子查询 虚拟表介绍
子查询 把一个sql语句放在括号里 ,这个括号里sql语句查询结果其实就是一张表,并且是一个临时在内存里存在的虚拟表 可以用括号把一个查询sql语句括起来 得到查询的结果并且用as 为这张虚拟表起个别 ...
- 【深入理解javascript】this的用法
引用:this的用法 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不了 情况1:构造函数 函数作为构造函数用,那么其中的this就代表它即将new出来的对象.另外 ...
- matlab做曲线拟合
python 做曲线拟合 https://blog.csdn.net/qq_16583687/article/details/72723708 matlab做拟合函数,可以在命令行输入:数据x,数据y ...
- PAT The Best Rank[未作]
1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we ...
- HTML <input> <button> <submit>
定义和用法 value 属性规定与按钮关联的初始值.请始终为按钮规定 type 属性,如果不填默认值是 "submit".submit其实就是一个特殊的button. <bu ...
- Locust性能测试5-参数化批量注册
前言 实现场景:所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复. 例如,模拟10用户并发注册账号,总共有100个手机号,要求注册账号不重复,注册完毕后结束测试 准备数据 虚拟用户 ...
- svg绘图工具raphael.js的使用
1.raphael.js svg画图的开源库,支持IE8+ 官方api: http://dmitrybaranovskiy.github.io/raphael/reference.html Githu ...