先前我们先看下这个命令:Spring Boot Maven Plugin中的spring-boot:run看下官方的解释:
spring-boot:run
Full name:
org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run
Description:
Run an executable archive application.
Attributes:
- Requires a Maven project to be executed.
- Requires dependency resolution of artifacts in scope: test.
- Since version: 1.1.
- Binds by default to the lifecycle phase: validate.
- Invokes the execution of the lifecycle phase test-compile prior to executing itself
他是一个运行可执行应用程序的命令,
当你使用这个命令时会输出以下信息:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.kayleoi:spring-boot-web-jsp >-------------------
[INFO] Building spring-boot-web-jsp 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) > test-compile @ spring-boot-web-jsp >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-web-jsp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-web-jsp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to F:\Java\spring-boot\spring-boot-web-jsp\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-boot-web-jsp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\Java\spring-boot\spring-boot-web-jsp\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-boot-web-jsp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\Java\spring-boot\spring-boot-web-jsp\target\test-classes
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) < test-compile @ spring-boot-web-jsp <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) @ spring-boot-web-jsp ---
这是把他先打成了一个jar了然后又执行了内置的tomcat。
接下来看目录结构:
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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kayleoi</groupId>
<artifactId>spring-boot-web-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-web-jsp</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-starter-tomcat</artifactId>
<scope>provided</scope>
</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</artifactId>
</dependency>
<!--用于编译jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller层:
package com.kayleoi.springbootwebjsp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value = "abc")
public String hello(Model model){
System.out.println("方法L");
model.addAttribute("msg","你好");
return "success";
}
}
application.yml:配置view在哪个位置和访问的路径
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/jsp
通过这样的配置直接启动即可:
访问:
这样所有的web的基本结束。接下来我们看springboot与数据访问和缓存的使用。