Spring Boot教程(二十一)开发Web应用(2)
2024-09-05 17:39:31
在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面。
@Controller
public class HelloController { @RequestMapping("/")
public String index(ModelMap map) {
// 加入一个属性,用来在模板中读取
map.addAttribute("host", "http://blog.didispace.com");
// return模板文件的名称,对应src/main/resources/templates/index.html
return "index";
} }
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>
如上页面,直接打开html页面展现Hello World,但是启动程序后,访问http://localhost:8080/
,则是展示Controller中host的值:http://blog.didispace.com
,做到了不破坏HTML自身内容的数据逻辑分离。
更多Thymeleaf的页面语法,还请访问Thymeleaf的官方文档查询使用。
Thymeleaf的默认参数配置
如有需要修改默认配置的时候,只需复制下面要修改的属性到application.properties
中,并修改成需要的值,如修改模板文件的扩展名,修改默认的模板路径等。
# Enable template caching.
spring.thymeleaf.cache=true
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value.
spring.thymeleaf.content-type=text/html
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true
# Template encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
支持JSP的配置
Spring Boot并不建议使用,但如果一定要使用,可以参考此工程作为脚手架:JSP支持
最新文章
- MySQL中的全文索引
- BZOJ1057[ZJOI2007]棋盘制作 [单调栈]
- sdut 1465 公共因子
- iOS开发拓展篇—应用之间的跳转和数据传递
- 进程间的通讯(IPC)方式
- DESCryptoServiceProvider加密、解密
- Careercup - Google面试题 - 4857362737266688
- Android网络框架---OkHttp3
- android studio 改变主题及字体大小
- java 为pdf添加水印图片
- Android JSON数据的读取和创建
- 标准C编程-笔记全集
- 如何修改"DEDECMS 提示信息!"方法!
- 多线程编程 NSOperation
- python基础--numpy.random
- Spark思维导图之资源调度
- 【XAF问题】不能将值NULL插入列";Oid";
- Python-列表-9
- LOJ.117.[模板]有源汇有上下界最小流(Dinic)
- Mybatis中sql语句中的in查询,判断null和size为0的情况
热门文章
- Sqlserver限制用户访问指定数据库
- MySQL5.6解压版服务无法启动—系统错误1067
- 洛谷 P4779 单源最短路径(标准版) 题解
- 3D max导出的设置选项
- 在springboot中集成mybatis开发
- 解决 java.security.cert.CertificateException: java.lang.IllegalArgumentException: Invalid input to toASCII:
- $id(id)函数
- TanksWar(坦克大战三维、二维版以及90版)
- Linux下关闭Tomcat
- node模块管理