提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
本文大概内容:
- 浏览器直接访问静态页面
- 通过请求访问页面
提示:以下是本篇文章正文内容,下面案例可供参考。
SpringBoot 默认是不支持Jsp的,所以集成Jsp需要做一些配置
一、整体步骤
- 创建SpringBoor基础项目
- 修改pom.xml,引入相关依赖
- 引入
spring-boot-starter-web
依赖 - 引入
jstl
依赖 - 引入
tomcat-embed-jasper
依赖
- 修改application.properties,添加视图解析配置
- 创建webapp目录,并新建jsp页面
- 新建Controller,添加访问接口
- 启动方式一,启动类启动(需要修改启动类的配置)
- 启动方式二,maven插件启动(项目需要添加插件依赖)
- 浏览器访问测试
- 项目打jar,需要添加静态资源配置
二、具体步骤
1. 创建SpringBoor基础项目
- 修改pom.xml,引入相关依赖
- 引入
spring-boot-starter-web
依赖 - 引入
jstl
依赖 - 引入
tomcat-embed-jasper
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.41</version>
</dependency>
- 修改application.properties,添加视图解析配置
#前缀
spring.mvc.view.prefix=/
#文件后缀
spring.mvc.view.suffix=.jsp
#jsp文件修改热部署
server.servlet.jsp.init-parameters.development=true
- 创建webapp目录,并新建jsp页面
<%@page isELIgnored="false" pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>hello,jsp访问成功!</h1>
<h1>hello,jsp访问成功!</h1>
<h1>hello,jsp访问成功!</h1>
<h1>hello,jsp访问成功!</h1>
</body>
</html>
- 新建HelloController.java,添加访问接口
/index
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author yejx
* @date 2021-05-26 14:41
*/
@Controller
public class HelloController {
@RequestMapping("/index")
public String index(){
return "index";
}
}
-
启动方式一,启动类启动(需要修改启动类的配置)
打开启动类Edit Configurations...
修改Working directory
-
启动方式二,maven插件启动(项目需要添加插件依赖)
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
- 浏览器访问测试
http://localhost:8080/index - 项目打jar,需要添加静态资源配置,修改pom.xml
<resources>
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
总结
错误不足之处还烦请指出,谢谢。
本文对应demo在仓库中tigerkin02-02目录中:gitee仓库地址