SpringBoot整合WEB页面

pom.xml引入

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>
  <dependencies>
    <!-- SpringBoot web 核心组件 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <!-- SpringBoot 外部tomcat支持 -->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
  </dependencies>

 

controller控制器代码

package com.xyt.springboot.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@SpringBootApplication
public class IndexController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    public static void main(String[] args) {
        SpringApplication.run(IndexController.class, args);
    }
}

 

application.yml配置文件代码

spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

jsp前端页面代码

<%--
  Created by IntelliJ IDEA.
  User: 23044
  Date: 2019/11/14
  Time: 19:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringBoot2.0</title>
</head>
<body>
<h1>SpringBoot2.0整合jsp页面,注意事项:当前项目类型一定要是war类型的,否则无法访问页面</h1>
</body>
</html>

 

注意事项:创建SpringBoot项目时,项目类型一定要是war类型的,否则无法访问页面

项目文件结构

项目启动结果

发布了31 篇原创文章 · 获赞 1 · 访问量 6531
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章