SpringBoot+Themeleaf的基本使用方法

第一步:用IDE创建一个普通maven工程,我用的eclipse.

第二步:修改pom.xml,加入支持SpringBoot和Themeleaf的依赖,文件内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4   <modelVersion>4.0.0</modelVersion>
 5 
 6   <groupId>com.chry</groupId>
 7   <artifactId>spring-boot-thymeleaf</artifactId>
 8   <version>0.0.1</version>
 9   <packaging>jar</packaging>
10 
11     <properties>
12         <java.version>1.7</java.version>
13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14     </properties>
15 
16   <name>spring-boot-thymeleaf</name>
17   <description>Spring Boot with Thymeleaf</description>
18 
19     <!-- Inherit defaults from Spring Boot -->
20     <parent>
21         <groupId>org.springframework.boot</groupId>
22         <artifactId>spring-boot-starter-parent</artifactId>
23         <version>1.4.0.RELEASE</version>
24     </parent>
25 
26   <dependencies>
27     <dependency>
28       <groupId>org.springframework.boot</groupId>
29       <artifactId>spring-boot-starter-thymeleaf</artifactId>
30     </dependency>
31    
32     <dependency>
33       <groupId>org.springframework.boot</groupId>
34       <artifactId>spring-boot-starter-test</artifactId>
35       <scope>test</scope>
36     </dependency>
37   </dependencies>
38  
39   <build>
40     <plugins>
41       <plugin>
42         <groupId>org.springframework.boot</groupId>
43         <artifactId>spring-boot-maven-plugin</artifactId>
44       </plugin>
45     </plugins>
46   </build>
47 
48 </project>


第三步:创建SpringBoot应用主类

package com.chry.springboot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SpringBootThymeleafApp {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootThymeleafApp.class, args);
    }
}


第四步: 创建HelloWorldController类, 返回值"index“将用于对映后面要创建的index.html

package com.chry.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}


第五步:在src/main/resources/ templates下创建要显示Hello World的index.html文件,加入 Themeleaf头,文件必须是 XHTML格式,否则运行会报异常。
由于XHTML格式检查很严格,文件大了不好查找。可以找一些在线格式化网站帮助检查,用XML格式检查就可以


第六步:用maven build工程, 生成spring-boot-thymeleaf-0.0.1.jar

第七步: 运行java -jar spring-boot-thymeleaf-0.0.1.jar, 然后浏览器http://localhost:8080 看效果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值