栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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




    
    he


hello 请求页面


4、index.html




    
    index


    hello idea2.4


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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357831.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号