欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot使用thymeleaf跳转页面实例代码

程序员文章站 2024-03-01 16:22:52
前言 在学习springboot 之后想结合着html做个小demo,无奈一直没掌握窍门,在多番的搜索和尝试下终于找到了配置的方法,使用thymeleaf做事前端页面模板...

前言

在学习springboot 之后想结合着html做个小demo,无奈一直没掌握窍门,在多番的搜索和尝试下终于找到了配置的方法,使用thymeleaf做事前端页面模板,不能使用纯html.

thymeleaf介绍

thymeleaf是面向web和独立环境的现代服务器端java模板引擎。

thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏览器中正确显示html,还可以作为静态原型工作,从而在开发团队中进行更强大的协作。

使用spring framework的模块,与您最喜爱的工具进行大量集成,以及插入自己的功能的能力,thymeleaf是现代html5 jvm web开发的理想选择,尽管它可以做的更多。

实战

项目结构

spring boot使用thymeleaf跳转页面实例代码 

thymeleaf pom依赖

<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>

模板页面

注意使用模板作为页面时候必须要把模板页面放在templates文件夹下

index.html

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>demo</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>my thymeleaf indexpage</h1>
<a href="/info/more" rel="external nofollow" >更多详情</a>
</body>
</html>

controller

@controller
public class pagecontroller {
  @requestmapping("/page")
  public string page3(model model){
    model.addattribute("username","张三");
    return "hello";
  }
  @requestmapping("info/more")
  public string page2(){
    return "hello2";
  }

  @requestmapping("sys/index")
  public string page(){
    return "sys/index";
  }
}


测试

spring boot使用thymeleaf跳转页面实例代码 

点击更多详情

spring boot使用thymeleaf跳转页面实例代码

项目源码: github地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。