前言
在学习springboot 之后想结合着html做个小demo,无奈一直没掌握窍门,在多番的搜索和尝试下终于找到了配置的方法,使用thymeleaf做事前端页面模板,不能使用纯html.
thymeleaf介绍
Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎。
Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 – 可以在浏览器中正确显示HTML,还可以作为静态原型工作,从而在开发团队中进行更强大的协作。
使用Spring Framework的模块,与您最喜爱的工具进行大量集成,以及插入自己的功能的能力,Thymeleaf是现代HTML5 JVM Web开发的理想选择,尽管它可以做的更多。
实战
项目结构
thymeleaf pom依赖
?
1
2
3
4
5
6
7
8
9
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-web</ artifactId >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-thymeleaf</ artifactId >
</ dependency >
|
模板页面
注意使用模板作为页面时候必须要把模板页面放在templates文件夹下
index.html
?
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE HTML>
< html xmlns:th = "http://www.thymeleaf.org" >
< head >
< title >demo</ title >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" />
</ head >
< body >
< h1 >my thymeleaf indexpage</ h1 >
< a href = "/info/more" rel = "external nofollow" >更多详情</ a >
</ body >
</ html >
|
controller
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Controller
public class PageController {
@RequestMapping ( "/page" )
public String page3(Model model){
model.addAttribute( "userName" , "张三" );
return "hello" ;
}
@RequestMapping ( "info/more" )
public String page2(){
return "hello2" ;
}
@RequestMapping ( "sys/index" )
public String page(){
return "sys/index" ;
}
}
|
测试
点击更多详情
项目源码: github地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.tuicool.com/articles/rU7RRbJ
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 49
-
2025-06-04 13
-
2025-05-25 25
-
2025-06-04 77
-
Armbian 系统 23.05 发布:基于 Debian 12,支持 i3 窗口管理器
2025-05-27 59
热门评论