0x00 教程内容
- 新建一个SpringBoot项目
- 启动项目
0x01 新建一个SpringBoot项目
1. 引入pom依赖
如果不同,可以对比一下(spring-boot-devtools 是热部署,不用到可以删掉。)
完整的 pom.xml
文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.9</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.shaonaiyi</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!--<scope>provided</scope> 注意,这个scope需要被注释掉--> </dependency> <!-- jsp标签库 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jstl的标签库--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
PS:如果只是想在项目中使用 JSP ,只需要加入 tomcat-embed-jasper 就可以了,但是如果想要使用 JSTL ,则还需要加入 jstl 和 taglibs.standard 这两个依赖。
2. 编写Controller代码
新建一个 controller 包,新建 IndexController 类,然后编写代码:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/index") public String index() { return "index"; } }
3. 编写jsp代码
- 在resource 文件夹下新建目录结构和文件
编写 index.jsp
文件的内容
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html lang="zh"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>首页</title> </head> <body> SpringBoot+JSP第一个例子 </body> </html>
编写web.xml
文件的内容
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> </web-app>
4. 编写配置文件
- 编写
application.properties
文件内容
#页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ #页面默认后缀目录 spring.mvc.view.suffix=.jsp
0x02 启动项目
1. 访问项目
访问端口为:http://localhost:8080/index
这样,就实现了!
0x03 实现增删改查
1. 访问项目
后端代码
0xFF 总结
- 本案例非常简单,没有复杂的内容,有基础的话,可以灵活扩展。