JavaWeb: Maven creates web project

1.1 WebProject structure

WebThe structure of the project is divided into: projects under development and Webprojects that can be deployed after development. The structures of these two projects are different. Let us introduce them one by one:

  • Maven WebProject Structure: Project in Development

    Insert image description here

  • Develop and deploy Webprojects

    Insert image description here

    • Develop the project by executing Maventhe packaging commandpackage, you can get the deployed Webproject directory
    • The compiled Javabytecode files and resourcesresource files will be placed in the directory WEB-INFbelowclasses
    • pom.xmlThe package corresponding to the dependency coordinates jarwill be placed in the directory WEB-INFbelowlib

1.2 Create Maven Webproject

After introducing Maven Webthe project structure, next use Mavento create Webthe 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 Mavenproject

2. Choose to use Webthe project skeleton, enter Maventhe project coordinates to create the project, and complete the project creation.

3. Delete pom.xmlredundant content

4. Complete Maven Webthe missing directory structure of the project

  1. Create Mavenproject

    Insert image description here

  2. Choose to use Webthe project skeleton, enter Maventhe project coordinates to create the project, and complete the project creation.

    Insert image description here

  3. Delete pom.xmlthe redundant content and leave only the following content. Pay attention to the difference between packaging methods jarandwar

    Insert image description here

  4. Complete Maven Webthe missing directory structure of the project. There is no javaand resourcesdirectory by default. You need to manually create and complete it. The final directory result is as follows

    Insert image description here

Not using skeleton

Specific steps include:

1.Create Mavenproject

2. Choose not to use Webthe project skeleton and enter Maventhe project coordinates to create the project

3. pom.xmlSet the packaging method towar

4. Complete the Maven Webmissing webappdirectory structure of the project

5. Complete the Maven Webmissing WEB-INF/web.xmldirectory structure of the project

  1. Create Mavenproject

    Insert image description here

  2. Choose not to use Webthe project skeleton and enter Maventhe project coordinates to create the project

    Insert image description here

  3. When pom.xmlsetting the packaging method war, the default is not to write it, which means the packaging method isjar

    Insert image description here

  4. Complete the missing directory structure Maven Webof the projectwebapp

    Insert image description here

  5. Complete the missing directory structure Maven Webof the projectWEB-INF/web.xml

    Insert image description here

  6. After the additions, the final project structure is as follows:

    Insert image description here

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 IDEAUseTomcat

  • Maven WebAfter the project is successfully created, you can package the project into a package by passing the command Maven, copy the file to the directory, start the project and deploy it successfully, and then access it through the browser.packagewarwarTomcatwebappsTomcat
  • 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 IDEAto use it quickly in China Tomcat?

There are two ways IDEAto integrate and use it Tomcat, namelyIntegrate localTomcatandTomcat Mavenplug-in

1.2.1 Integrate localTomcat

Tomcat9Goal: Integrate the local installation just now IDEAand complete the project deployment. Specific implementation steps

  1. Open the add local Tomcatpanel

    Insert image description here

  2. Specify the local Tomcatspecific path

    Insert image description here

  3. Modify Tomcatthe name. This step does not need to be changed. It just makes the name look more meaningful. HTTP portThe port in can also be modified, such as changing 8080 to 80.

    Insert image description here

  4. Deploy the development project Tomcatto

    Insert image description here

    Extended content: What is the difference xxx.warbetween xxx.war explodedthese two deployment project modes?

    • warThe mode is to WEBpackage the project warand warpublish the package to Tomcatthe server.

    • war explodedThe mode is to publish the project to the server WEBin the location of the current folder.Tomcat

    • warAfter the pattern is deployed successfully, there will be deployed project content in the Tomcatdirectorywebapps

    • war explodedAfter the pattern is deployed successfully, there is no directory in the Tomcatdirectory , but the content in the directory webappsof the project is used for deployment.target

    • It is recommended that everyone choose wara mode for deployment, which is more in line with the actual situation of project deployment.

  5. After the deployment is successful, you can start the project. In order to better see the startup effect, you can webappadd index.jspa page to the directory.

    Insert image description here

  6. After successful startup, you can access the test through the browser

    Insert image description here

  7. Final notes

    Insert image description here

1.3.2 Tomcat MavenPlug-ins

IDEAUsing local project deployment in Tomcatis relatively cumbersome, so we need a simpler way to replace it, which is to directly use the plug-in in Mavento Tomcatdeploy the project. The specific implementation steps only require two steps, respectively. yes:

  1. pom.xmlAdd Tomcatplugin in

    <build>
        <plugins>
        	<!--Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
    
  2. Use Maven Helperthe plug-in to quickly start the project, select the project,右键-->Run Maven --> tomcat7:run

Insert image description here

Notice:

  • If you cannot see the Run Mavenand after selecting the project and right-clicking Debug Maven, you need to IDEAdownload Maven Helperthe plug-in at this time. The specific operation method is: File --> Settings --> Plugins --> Maven Helper ---> InstallAfter installation, restart according to the prompts IDEA, and you can see it.

Insert image description here

  • Maven TomcatThe plug-in currently only has Tomcat7one version, and no higher version can be used.
  • Using Maven Tomcatplug-ins, if you want to modify Tomcatthe 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>

Guess you like

Origin blog.csdn.net/qq_37726813/article/details/128497826