SpringBoot 如何返回页面

背景

@ResponseBody 注解应用在一个控制器方法上时,SpringMVC会将该方法的返回对象直接写入HTTP响应体中,而不是经过视图解析器查找和渲染视图。 故如下组合不会返回jsphtml

# 场景1 :@RestController = @ResponseBody + @Controller
@RestController
public class CalculatorController {

# 场景2
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
@ResponseBody
public double addNumbers(@RequestParam("num1") double num1, @RequestParam("num2") double num2) {

Further Reading : What is @ResponseBody does?

解决办法

①去除@ResponseBody或将含有Rest的注解换成对应的原始注解;
②不通过String返回,通过ModelAndView对象返回,上述例子可将return语句换成return new ModelAndView("index")

在这里插入图片描述

前提

第1步:编写前端页面
在这里插入图片描述

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

第2步:编写Controller层
在这里插入图片描述

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

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

在这里插入图片描述

不使用模板引擎的情况下,访问页面的方法有两种:

1)将所需要访问的页面放在resources/static/文件夹下,这样就可以直接访问这个页面。

# 当return "index.html";时
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        成功返回

# 当return "redirect:index.html";时
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        成功返回

而同样在resources,访问的页面放在resources/templates/,则禁止访问这个页面

# 当return "index2.html";时
http://localhost:8080/index2.html  404返回
http://localhost:8080/map2         404返回

# 当return "redirect:index.html";时
http://localhost:8080/index2.html  404返回
http://localhost:8080/map2         404返回

[Ref] SpringBoot如何返回页面

2)使用redirect实现页面的跳转

注意:需要使用springmvc的配置,不然使用redirect也会报错,如上文所述
在这里插入图片描述

spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  web:
    resources:
      static-locations: classpath:/templates/,classpath:/static/
@Controller
public class HelloController {
    @RequestMapping("/map1")
    public String index() {
        return "redirect:index.html";
    }

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

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

# 当return "index.html";时
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        404返回  引入springmvc配置后,return "index.html"不起作用了

# 当return "redirect:index.html";时
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        成功返回

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

# 当return "index2.html";时
http://localhost:8080/index2.html  成功返回
http://localhost:8080/map2         404返回 引入springmvc配置后,return "index.html"不起作用了

# 当return "redirect:index.html";时
http://localhost:8080/index2.html  成功返回
http://localhost:8080/map2         成功返回

在这里插入图片描述

使用Thymeleaf模板引擎

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>
spring:
  thymeleaf:
    prefix: classpath:/static/
    suffix: .html
    cache: false #关闭缓存
@Controller
public class HelloController {
    @RequestMapping("/map1")
    public String index() {
        return "index";
    }

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

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

# 配置 prefix: classpath:/static/
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        成功返回  

# 配置 prefix: classpath:/templates/
http://localhost:8080/index.html  成功返回
http://localhost:8080/map1        404返回   

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

# 配置 prefix: classpath:/static/
http://localhost:8080/index2.html  404返回 
http://localhost:8080/map2         404返回 

# 配置 prefix: classpath:/templates/
http://localhost:8080/index.html  404返回 
http://localhost:8080/map1        成功返回  

在这里插入图片描述

返回效果演示

成功返回

在这里插入图片描述

404返回

在这里插入图片描述

参考

SpringBoot如何返回页面

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值