springboot中ModelAndView跳转设置

想要在springboot项目中跳转页面可以使用ModelAndView
前提是首先需要导入thymeleaf的依赖

	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <!-- 版本号是根据你的springboot项目的版本来 可以写也可以不写
            (不写默认就是根据创建的springboot项目本版本设置)-->
            <version>2.6.5</version>
 	</dependency>

如果不想用自带的版本 可以去 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
mvnrepository上面找到你想要的版本进行设置

thymeleaf默认的是找.html文件,在templates文件夹下面的 如果你想更改搜索的位置 你可以在application.yml或者application.properties 中设置
下面是application.yml的设置

thymeleaf:
	#这两个值都是默认的前后缀 可以根据需要自行更改
    suffix: .html
    prefix: classpath:/templates

跳转的路径写法
我要跳转的目标是:templates/admin/login.html
在这里插入图片描述
controller层写法就是:

@RequestMapping("/toLogin")
public ModelAndView loginPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/login");
        return modelAndView;
    }

这样就可以跳转过去

注意:如果找不到页面需要查看maven中是否加载了thymeleaf的依赖
在这里插入图片描述
再就是编译后的target中是否有跳转的页面
在这里插入图片描述
最后使用thymeleaf时前端加上下面的这个数据才能生效

xmlns:th="http://www.thymeleaf.org
<!DOCTYPE html>
<!--使用前端时记得加上这个 在html标签后加上 -->
<html xmlns:th="http://www.thymeleaf.org">

  <head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" 
          href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
  </head>

  <body>
  
    <p th:text="#{home.welcome}">Welcome to our grocery store!</p>
  
  </body>

</html>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值