SpringBoot如何返回页面

前提

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
你好!初学者,我是SpringBoot的简单启动页面!
</body>
</html>

在这里插入图片描述

@Controller
public class HelloController {
   
    @RequestMapping("/map1")
    public String index() {
   
        return "index.html";
    }

    @RequestMapping("/map2")
    public String map2() {
   
        return "index2.html";
    }
}

在这里插入图片描述

不使用 模板引擎

访问的页面放在resources/static/,就可直接访问这个页面

http://localhost:8080/index.html  成功返回

http://localhost:8080/map1        成功返回

访问的页面放在resources/templates/,禁止访问这个页面

http://localhost:8080/index2.html  404返回

http://localhost:8080/map1         404返回

[Ref] SpringBoot如何返回页面

在这里插入图片描述

使用 模板引擎

使用 springmvc 配置

如果使用spring-boot-starter-parent,就无需引入依赖

spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  web:
    resources:
      static-locations: classpath:/templates/,classpath:/static/

在这里插入图片描述

直接访问资源

http://localhost:8080/index.html    成功返回
http://localhost:8080/index.htm2    成功返回

http://localhost:8080/map1    报404
http://localhost:8080/map2    报404
调用接口需要 redirect
@Controller
public class HelloController {
   
    @RequestMapping("/map1")
    public String index() {
   
        return "redirect:index.html";
    }

    @RequestMapping("/map2")
    public String map2() {
   
        return "redirect:index2.html";
    }
}
http://localhost:8080/index.html    成功返回
http://localhost:8080/index.htm2    成功返回

http://localhost:8080/map1  成功返回
http://localhost:8080/map2  成功返回

使用Thymeleaf模板引擎

成功返回

在这里插入图片描述

404返回

在这里插入图片描述

相关推荐

  1. SpringBoot如何缓存方法返回值?

    2024-01-06 13:32:05       26 阅读
  2. uni-app 如何返回到指定的页面

    2024-01-06 13:32:05       46 阅读
  3. 如何在小程序中实现页面之间的返回

    2024-01-06 13:32:05       29 阅读
  4. 如何自定义SpringBoot的白标错误页面

    2024-01-06 13:32:05       29 阅读
  5. springboot返回Byte字节

    2024-01-06 13:32:05       27 阅读
  6. uniapp之页面返回并调用返回页方法

    2024-01-06 13:32:05       50 阅读
  7. CachedNetworkImage 在listview 返回页面闪烁问题

    2024-01-06 13:32:05       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 13:32:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 13:32:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 13:32:05       82 阅读
  4. Python语言-面向对象

    2024-01-06 13:32:05       91 阅读

热门阅读

  1. Vue将数据存放在会话存储中

    2024-01-06 13:32:05       66 阅读
  2. sqoop(DataX)-MySQL导入HIVE时间格问题

    2024-01-06 13:32:05       59 阅读
  3. 全志F1C100s Linux 系统编译出错:不能连接 github

    2024-01-06 13:32:05       71 阅读
  4. BAM文件数据结构详解

    2024-01-06 13:32:05       63 阅读