springBoot 集成 thymeleaf

一、背景

       springboot支持JSP.jsp)、thymeleaf.html)和freemarker.ftl)这三种前端界面展示形式。这篇文章简单讲述下springboot如何集成thymeleaf

二、集成步骤

2.1 添加 maven 依赖

<!-- 继承springboot,不加这个parent标签就会报错 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<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>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.2 配置 application.properties

# 是否启用模板缓存,开发配置为false,避免修改模板还要重启服务器
spring.thymeleaf.cache=false

# 在呈现模板之前检查模板是否存在。
spring.thymeleaf.check-template=true

# 是否检查模板位置是否存在
spring.thymeleaf.check-template-location=true

# Content-Type value.
spring.thymeleaf.content-type=text/html

# 是否启用thymeleaf
spring.thymeleaf.enabled=true

# 模板编码
spring.thymeleaf.encoding=UTF-8

# 应该从解决方案中排除的视图名称的逗号分隔列表。
#spring.thymeleaf.excluded-view-names=

# 模板的模式,支持 HTML, XML TEXT JAVASCRIPT
spring.thymeleaf.mode=HTML5

# 配置模板路径,默认是templates,可以不用配置
spring.thymeleaf.prefix=classpath:/templates/

# 设定模板的后缀.
spring.thymeleaf.suffix=.html

# 链中模板解析器的顺序。
#spring.thymeleaf.template-resolver-order=

# 可以解析的视图名称的逗号分隔列表
#spring.thymeleaf.view-names=

       工程的目录结构如下:

2.3 编写相关代码和文件

       TestController 代码如下:

@Controller
@RequestMapping("/html")
public class TestController {

	@RequestMapping("/hello")
	public String hello(Model model) {
		model.addAttribute("name", "world");
		return "hello";
	}
}

        App 代码如下:

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	//将springboot应用驱动起来
        SpringApplication.run(App.class, args);
    }
}

       hello.html 的代码如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
</head>
<body>
    <!--/*@thymesVar id="name" type="java.lang.String"*/-->
    <p th:text="'Hello, ' + ${name}" ></p>
</body>
</html>

       在界面请求 http://localhost:8080/html/hello 即可:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值