SpringBoot在无网络的环境下创建项目
首先创建一个maven项目,在pom.xml中引用相关依赖
<!-- 如有parent需要替换成Springboot的parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 如需要war包这里jar改成war -->
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 视图模板(不是必须的,根据需求添加) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 解除模板强制验证(不是必须的,根据需求添加) -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后在java文件夹中新建一个包用于存放启动类,如果直接在src下创建启动类运行中可能会出现错误,在新建的包里面创建启动类
package my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class, args);
}
}
然后在之前创建的包下面创建放控制器的文件夹controller,并新建一个控制器类
package my.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class TestController {
@RequestMapping(value = "/")
public ModelAndView index(){
return new ModelAndView("index");
}
@RequestMapping(value = "/api.html")
public String test(){
return "test";
}
}
在resource下可以新建两个文件夹static和templates用于存房静态资源和页面,另新建一个application.yml或application.properties配置文件
application.properties内容如下
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.mode=LEGACYHTML5
项目结构如下
index.html内容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
1111<br>
<img src="1.jpg">
</body>
</html>
运行启动类
完成!!!