springboot2整合thymeleaf访问html文件,有时404

由于会老是出现访问404,所以将能正常访问的示例贴出来:

出现404得可能情况:

  • 不能加@responsebody否则返回的是字符串
  • 其实中间得thymeleaf配置可以不配,有些是默认的,但是如果配错了,比如少了classpath,那就会出现404找不到问题
  • 还有一种可能是jar没有完全加载,刷新一下maven仓库
  • 另外实在检查了都没问题的话,换jar包试试

yml文件:

server:
  port: 8088
  servlet:
    context-path: /boot2
spring:
  application:
    name: boot2
  thymeleaf:
    prefix: classpath:templates/
    suffix: .html
    servlet:
      content-type: text/html
  cache: false

在这里插入图片描述

@Controller
public class IndexController {

    @GetMapping(value = "index")
    public String index(){
        System.out.println(111);
        //return new ModelAndView("hello");
        return "hello";
    }

    @GetMapping("/aaa")
    @ResponseBody
    public String aaa(){
        return "aaa";
    }
}
       <dependency>
			   <groupId>org.springframework.boot</groupId>
			   <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

    <!--    <dependency>-->
    <!--        <groupId>org.thymeleaf</groupId>-->
    <!--        <artifactId>thymeleaf-spring5</artifactId>-->
    <!--    </dependency>-->
发布了23 篇原创文章 · 获赞 5 · 访问量 4831

猜你喜欢

转载自blog.csdn.net/weixin_42567141/article/details/104240585
今日推荐