Springboot 返回html页面

一. 不使用templates模板情况下

SpringBoot里面没有我们之前常规web开发的WebContent(WebApp),它只有src目录

在src/main/resources下面有两个文件夹,static和templates   springboot默认  static中放静态页面,而templates中放动态页面

 

静态页面:

 这里我们直接在static放一个index.html,然后直接输入http://localhost:8080/index.html便能成功访问到static中的index.html

(好像可以新建一个public文件夹,也可以放静态文件)

 

也可以通过controller跳转:

 

@RequestMapping(value = "/test23")
public String index() {
    System.out.println("++++++++++++++++++");
    return "index.html";
}

然后输入http://localhost:8080/test23就可以成功访问到static中的index.html

但是如果代码写成

@RequestMapping(value = "/test23")
public String index() {
    System.out.println("++++++++++++++++++");
    return "index";
}

或者

return "redirect.html"

或者

return "redirect:index"

然后输入http://localhost:8080/test23后都会报错404

那么现在application.yml中加上如下一代码

spring:
  mvc:
    view:
      prefix: resources/static/
      suffix: .html

重新进行以上的测试。

http://localhost:8080/index.html 这个地址直接访问的还是static下的index.html

return "index.html";  页面访问http://localhost:8080/test23还是404

return "index"; 页面访问http://localhost:8080/test23还是404

return "redirect:index";页面访问http://localhost:8080/test23还是404
return "redirect.html";页面访问http://localhost:8080/test23还是404

 

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值