springboot使用thymeleaf模板引擎,controller返回视图到templates/**.html报404

  1. 错误
    使用模板引擎访问路径页面返回404

    pom文件
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf</artifactId>
            </dependency>

    controller类中是这么写的
        @RequestMapping("test")
        public String test() {
            System.out.println("test方法进入了");
            System.out.println("test方法进入了");
            return "success";
        }
    
  2. 分析原因
      @RequestMapping("/fzf")
        public String fzf() {
            return "/templates/index.jsp";
        }

    添加这个测试发现能找到文件,怀疑是引擎配置未生效在application.yml中添加

    #配置支持thymeleaf模板引擎
    thymeleaf:
      cache: false
      prefix: classpath:templates/
      suffix: .html
      #配置静态资源的位置
    mvc:
      static-path-pattern: /**
    resources:
      static-locations: classpath:/static/

    仍然404,再检查启动类所在包是否包含了controller层等代码,发现正常.
    在将文件位置移到static静态资源文件夹下发现能够访问.为什么模版引擎不能正常使用呢.发现导入的包不对,正确的包应该是下面这个

            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf-spring5</artifactId>
            </dependency>

    配置后页面跳转成功
     

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值