SpringBoot 开发使用thymeleaf模板开发html页面

springboot技术学习 https://www.itkc8.com

建立一个Springboot maven项目。文件目录就如下:

1-1

接下来呢就是pom文件映入相关jar包了。一定要引入thymeleaf。(更多内容看文章最后的github链接,项目已上传至github)

 

[html] view plain copy

  1. <properties>  
  2.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.   
  4.         <!--引入themeleaf-->  
  5.         <thymeleaf.version> 3.0.2.RELEASE </thymeleaf.version>  
  6.         <thymeleaf-layout-dialect.version> 2.1.1 </thymeleaf-layout-dialect.version>  
  7.     </properties>  
  8.   
  9.   
  10.   
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>org.springframework.boot</groupId>  
  14.             <artifactId>spring-boot-starter-web</artifactId>  
  15.         </dependency>  
  16.   
  17.         <dependency>  
  18.             <groupId>org.springframework.boot</groupId>  
  19.             <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  20.             <version>1.0.2.RELEASE</version>  
  21.         </dependency>  
  22.     </dependencies>  

接下来就是配置了,在resource文件夹下创建一个application文件配置thymeleaf

1-2

配置完成后开始代码的撰写,首先是controller层,我写了三个方法,首页,跳转,和视图的三种。源码如下:

 

[java] view plain copy

  1. package com.thymeleaf.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.ui.Model;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. /** 
  8.  * Created by zhouhaishui on 2017/5/2. 
  9.  */  
  10. @Controller  
  11. public class pageController {  
  12.   
  13.     /** 
  14.      * 首页 
  15.      * @return 
  16.      */  
  17.     @RequestMapping("/")  
  18.     public String page(){  
  19.         return "system/index";  
  20.     }  
  21.   
  22.   
  23.     /** 
  24.      * 跳转 
  25.      * @return 
  26.      */  
  27.     @RequestMapping("/redirect")  
  28.     public String page2(){  
  29.         return "redirect/redirect";  
  30.     }  
  31.   
  32.   
  33.     /** 
  34.      *视图 
  35.      * @param model 
  36.      * @return 
  37.      */  
  38.     @RequestMapping("/model")  
  39.     public String page3(Model model){  
  40.         model.addAttribute("name","seawater");  
  41.         return "hello";  
  42.     }  
  43. }  

 

在resource文件夹下按照自己的需要建立文件夹html文件。我这里用改的就三个页面所以就没有那么多啦,大家根据自己的需要去建立。

 

1-3

然后就运行就可以了。

页面如下:

2-1

2-3

2-2

所谓的不可以跳转是不存在的,只是没有配置对呢。附上项目源码:有兴趣的可以去github上看看这个thymeleaf项目。

当然html中一些写法是需要参考thymeleaf的规则的,这里就不详细描述,可以到thymeleaf官方网站查看。

 

springboot技术学习 https://www.itkc8.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值