项目结构
1.新建springboot项目,添加web的起步依赖,其他依赖需要手动添加
不加版本号会导致导出的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 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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.it</groupId>
<artifactId>026-springboot-jar</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--tomcat依赖,处理jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--指定打包后的文件名称-->
<finalName>myboot</finalName>
<!--resources插件,把jsp编译到指定的目录-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<!--使用了mybatis,而且mapper文件放在src/main/java目录中-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--把src/main/resources下面的所有文件,都包含到classes目录-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--打包jar,有jsp文件时,必须来指定maven-plugin插件的版本是1.4.2.RELEASE-->
<version>1.4.2.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
2.application.properties文件
server.port=9002
server.servlet.context-path=/myboot
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
3.main.jsp文件
4.HelloController
package com.it.controller;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("data","springboot打包为jar");
modelAndView.setViewName("main");
return modelAndView;
}
}
5.Application类,运行项目,进行项目测试
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
项目测试:
1.首先测试编写好的项目在idea中的Tomcat启动后,是否可以正常运行
2. 测试成功后进行clean和package
3.生成打包文件后,右键选择打开myboot.jar所在文件的位置
4.进入该jar包所在文件的位置,在地址栏输入cmd,打开命令行窗口
5.输入编译该文件的代码,点击回车,独立的Tomcat运行该jar包。(注意:该命令提示行窗口不能关闭,关闭会导致服务结束)
6. 直接在网站中输入相应的路径地址进行访问