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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章