springboot项目简单的页面默认跳转方式总结:RequestMapping(“/helllo“)使用String字符串 根据return “he“; 跳转到 he.html页面。简单记录目录结构

一、前言
1、当前项目图片
在这里插入图片描述
二、主要错误
1、错误Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Oct 30 15:58:03 CST 2021 There was an unexpected error (type=Not Found, status=404).
2、原因
一般是目录结构错误

三、项目代码(主要的)
1、HelloController.java

package com.feng.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

#控制层添加Controller注解
@Controller
public class HelloController {
    
    

    @RequestMapping("/hello")
//    @GetMapping("/hello")
    //直接return到he.html页面
    public String helloTest(){
    
    
//        return "he.html";
        return "/he";
    }

    //rest风格的代码
    @GetMapping("/hi")
    //通过Json格式的形式传递Json字符串
    @ResponseBody
    public String test2(){
    
    
        return "hi, kangkang.";
    }
}

2、application.yaml

spring:
  mvc:
    view:
    # Controller里面使用String字符串进行页面跳转的时候return里面不用写.html
      suffix: ".html"
    # return 里面的页面放置位置,默认static、public,添加了prefix就会在在路径下添加test2路径
      prefix: test2

server:
# 修改tomcat启动端口号
  port: 8087
#最下面两行添加了后,访问路径就是下面这个样子,content-path是修改访问路径的
  #http://localhost:8087/public/hello
#  servlet:
#    context-path: /public

3、he.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>he</title>
</head>
<body>
hello 请求页面
</body>
</html>

4、index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    hello idea2.4
</body>
</html>

四、介绍
如第三点代码里面的注释。

五、后话
http://localhost:8087
项目启动后会默认访问static里面的index.html页面

Supongo que te gusta

Origin blog.csdn.net/qq_43987149/article/details/121052298
Recomendado
Clasificación