spring boot thymeleaf controller 怎么间接跳转

2025-05-17 02:12:44
推荐回答(1个)
回答(1):

在学习springboot 之后想结合着html做个小demo,无奈一直没掌握窍门,在多番的搜索和尝试下终于找到了配置的方法,使用thymeleaf做事前端页面模板,不能使用纯html.如果使用纯html的话,推荐使用angular JS 或者vue js ,使用它们自带的前端路由进行页面跳转

thymeleaf介绍

Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎。

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

使用Spring Framework的模块,与您最喜爱的工具进行大量集成,以及插入自己的功能的能力,Thymeleaf是现代HTML5 JVM Web开发的理想选择,尽管它可以做的更多。

实战

项目结构

项目结构

thymeleaf pom依赖



org.springframework.boot

spring-boot-starter-web





org.springframework.boot

spring-boot-starter-thymeleaf



模板页面

注意使用模板作为页面时,要把模板页面放在templates文件夹下,其实也不一定要放在templates文件夹下,可以在yml配置文件中指明存放的文件夹,只是习惯使用templates文件夹作为模板存放路径.

index.html







demo







my thymeleaf indexpage



更多详情





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

}

}