1.1 Web
Project structure
Web
The structure of the project is divided into: projects under development and Web
projects that can be deployed after development. The structures of these two projects are different. Let us introduce them one by one:
-
Maven Web
Project Structure: Project in Development -
Develop and deploy
Web
projects- Develop the project by executing
Maven
the packaging commandpackage
, you can get the deployedWeb
project directory - The compiled
Java
bytecode files andresources
resource files will be placed in the directoryWEB-INF
belowclasses
pom.xml
The package corresponding to the dependency coordinatesjar
will be placed in the directoryWEB-INF
belowlib
- Develop the project by executing
1.2 Create Maven Web
project
After introducing Maven Web
the project structure, next use Maven
to create Web
the project. There are two ways to create the project: using the skeleton and not using the skeleton. The version used idea
:IntelliJ IDEA 2022.3 (Ultimate Edition)
Use skeleton
Specific steps include:
1.Create
Maven
project2. Choose to use
Web
the project skeleton, enterMaven
the project coordinates to create the project, and complete the project creation.3. Delete
pom.xml
redundant content4. Complete
Maven Web
the missing directory structure of the project
-
Create
Maven
project -
Choose to use
Web
the project skeleton, enterMaven
the project coordinates to create the project, and complete the project creation. -
Delete
pom.xml
the redundant content and leave only the following content. Pay attention to the difference between packaging methodsjar
andwar
-
Complete
Maven Web
the missing directory structure of the project. There is nojava
andresources
directory by default. You need to manually create and complete it. The final directory result is as follows
Not using skeleton
Specific steps include:
1.Create
Maven
project2. Choose not to use
Web
the project skeleton and enterMaven
the project coordinates to create the project3.
pom.xml
Set the packaging method towar
4. Complete the
Maven Web
missingwebapp
directory structure of the project5. Complete the
Maven Web
missingWEB-INF/web.xml
directory structure of the project
-
Create
Maven
project -
Choose not to use
Web
the project skeleton and enterMaven
the project coordinates to create the project -
When
pom.xml
setting the packaging methodwar
, the default is not to write it, which means the packaging method isjar
-
Complete the missing directory structure
Maven Web
of the projectwebapp
-
Complete the missing directory structure
Maven Web
of the projectWEB-INF/web.xml
-
After the additions, the final project structure is as follows:
The web projects created by the above two methods are not complete, and the content needs to be supplemented manually. As for which method is finally used to create the project Maven Web
, it is OK. You can choose to use it according to your own preferences.
1.3 IDEA
UseTomcat
Maven Web
After the project is successfully created, you can package the project into a package by passing the commandMaven
, copy the file to the directory, start the project and deploy it successfully, and then access it through the browser.package
war
war
Tomcat
webapps
Tomcat
- However, during the development process, the content of the project will often change. It is very inconvenient to deploy tests in the above way.
- How
IDEA
to use it quickly in ChinaTomcat
?
There are two ways IDEA
to integrate and use it Tomcat
, namelyIntegrate localTomcat
andTomcat Maven
plug-in
1.2.1 Integrate localTomcat
Tomcat9
Goal: Integrate the local installation just now IDEA
and complete the project deployment. Specific implementation steps
-
Open the add local
Tomcat
panel -
Specify the local
Tomcat
specific path -
Modify
Tomcat
the name. This step does not need to be changed. It just makes the name look more meaningful.HTTP port
The port in can also be modified, such as changing 8080 to 80. -
Deploy the development project
Tomcat
toExtended content: What is the difference
xxx.war
betweenxxx.war exploded
these two deployment project modes?-
war
The mode is toWEB
package the projectwar
andwar
publish the package toTomcat
the server. -
war exploded
The mode is to publish the project to the serverWEB
in the location of the current folder.Tomcat
-
war
After the pattern is deployed successfully, there will be deployed project content in theTomcat
directorywebapps
-
war exploded
After the pattern is deployed successfully, there is no directory in theTomcat
directory , but the content in the directorywebapps
of the project is used for deployment.target
-
It is recommended that everyone choose
war
a mode for deployment, which is more in line with the actual situation of project deployment.
-
-
After the deployment is successful, you can start the project. In order to better see the startup effect, you can
webapp
addindex.jsp
a page to the directory. -
After successful startup, you can access the test through the browser
-
Final notes
1.3.2 Tomcat Maven
Plug-ins
IDEA
Using local project deployment in Tomcat
is relatively cumbersome, so we need a simpler way to replace it, which is to directly use the plug-in in Maven
to Tomcat
deploy the project. The specific implementation steps only require two steps, respectively. yes:
-
pom.xml
AddTomcat
plugin in<build> <plugins> <!--Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build>
-
Use
Maven Helper
the plug-in to quickly start the project, select the project,右键-->Run Maven --> tomcat7:run
Notice:
- If you cannot see the
Run Maven
and after selecting the project and right-clickingDebug Maven
, you need toIDEA
downloadMaven Helper
the plug-in at this time. The specific operation method is:File --> Settings --> Plugins --> Maven Helper ---> Install
After installation, restart according to the promptsIDEA
, and you can see it.
Maven Tomcat
The plug-in currently only hasTomcat7
one version, and no higher version can be used.- Using
Maven Tomcat
plug-ins, if you want to modifyTomcat
the port and access path, you can modify it directlypom.xml
<build>
<plugins>
<!--Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port><!--访问端口号 -->
<!--项目访问路径
未配置访问路径: http://localhost:80/tomcat-demo2/a.html
配置/后访问路径: http://localhost:80/a.html
如果配置成 /hello,访问路径会变成什么?
答案: http://localhost:80/hello/a.html
-->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>