需求

在本地没有安装Tomcat,也可以运行Tomcat的项目,最好是在maven中集成Tomcat,这样加载一下maven的依赖就可以了。

项目实现

创建maven项目

创建maven项目时,需要设置一些必须的配置。

  1. JDK
  2. Archetype

JDK版本根据自己的需求选择,我这里选择JDK11.

Archetype需要选择org.apache.maven.archetypes:maven-archetype-webapp,版本选择1.0

01-新建项目-20230201-790

添加Tomcat依赖

打开项目的pom.xml文件

02– pom.xml 20230201-903

添加Tomcat7插件

  • port:表示设置访问的端口,可以是80或者其他都可以。
1
2
3
4
5
6
7
8
9
10
11
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8090</port>
<path>/</path> <!--项目访问路径。当前配置的访问是localhost:9090/, 如果配置是/aa,则访问路径为localhost:9090/aa -->
</configuration>
</plugin>
</plugins>

添加完插件记得右键重新加载maven项目

添加运行配置

03-springmvc – pom.xml 20230201-311

选择Maven配置

04-运行调试配置20230201-242

设置配置

选中运行的项目,我这里选择模块springmvc-04-request-param,选择运行项目的工作目录的根目录,再添加运行命令

1
tomcat7:run

05-运行调试配置20230201-815

查看运行情况

打开网址,第一个页面默认是webapp/index.jsp文件。

06-springmvc – pom.xml 20230201-403