9. Spring Boot使用thymeleaf模板
来源:互联网 发布:建站后seo 编辑:程序博客网 时间:2024/06/15 22:28
本章节主要讲在Spring Boot如何使用模板thymeleaf(内置模板),官方也推荐使用thymeleaf模板引擎作为前端模板。
1. 在pom.xml中加入jar包依赖
<!-- thymeleaf模板 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
2. 在application.properties中添加 thymeleaf 配置
######## THYMELEAF(ThymeleafAutoConfiguration) ##############spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8# ;charset=<encoding>is addedspring.thymeleaf.content-type=text/html;charset=UTF-8# set to false for hot refreshspring.thymeleaf.cache=false
3. 在src/main/resources/templates下创建hello.html文件
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head><title>Hello World!</title></head><body><h1 th:inline ="text">Hello.v.2</h1><p th:text="${hello}"></p></body></html>
其中th:xx属性在后面会有相关介绍,这里不做讨论。
4. 编写Controller
package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/template")public class TemplateController {@RequestMapping("/hello")public String helloTemplate(ModelMap model) {//set valuemodel.put("hello", "this is my first template");return "/hello";}}
说明:必须使用
必须使用@Controller注解,不能使用@RestController。
在Controller中使用 @RestController 注解,该注解是Spring 4.0引入的。查看源码可知其包含了 @Controller 和 @ResponseBody 注解。我们可以理解为 @Controller的增强版。专门为响应内容式的 Controller 而设计的,可以直接响应对象为JSON。
而 @Controller 用来响应页面,spring-boot 支持多种模版引擎包括:
1,FreeMarker
2,Groovy
3,Thymeleaf (Spring 官网使用这个)
4,Velocity
5,JSP (貌似Spring Boot官方不推荐,STS创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的webapp目录)
不过本文还是选择大家都熟悉的JSP来举例,因为使用JSP与默认支持的模版需要特殊处理,所以拿来举例更好。
关于Controller 方法可以接收参数使用@RequestBody、@RequestParam、@ModelAttribute、JSONObject、HttpEntity 等方式,皆与Spring的使用一样,这里不做赘述。
5. 在浏览器中输入 http://localhost:8080/template/hello, 可以看到
Hello.v.2
this is my first template
后面会详细介绍thymeleaf模板的相关知识。
@Controller
- 9. Spring Boot使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- Spring Boot使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring boot使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- Spring Boot 使用 Thymeleaf模板
- spring-boot--使用thymeleaf模板
- Spring boot使用thymeleaf模板
- Spring-boot中thymeleaf模板使用小结
- (8)spring boot使用thymeleaf模板引擎
- Spring Boot使用Thymeleaf
- spring-boot--整合thymeleaf模板
- spring-boot thymeleaf模板学习
- spring-boot--整合thymeleaf模板
- Spring Boot–thymeleaf模板
- 迷茫的程序生活
- UVALive
- JS之文档对象模型DOM
- React + nodemailer + koa-jwt 实现登录注册邮箱验证
- 对图片“懒加载”的一些理解
- 9. Spring Boot使用thymeleaf模板
- Ubuntu14.04 安装及使用:[2]双系统安装
- Mapped Statements collection already contains value for xxxx.xxx.xxxx
- 源码android6.0 系统应用时钟--详细的闹钟分析
- Number Sequence
- 蓝信系统助力华电红雁池电厂建设
- Bitmap的内存优化
- Ubuntu下搜狗输入法只有悬浮框不能输入中文
- C++运行报错 HEAP CORRUPTION DELTECTED|Run-Time Check Failure #2