Having configured the basic development environment in the previous section, I will now use IDEA to build a Web project.

Introduce a Maven

Maven is a Java project package management and build tool. Its main functions are:

  • Standardized project structures;
  • Standardized build processes (compile, test, package, release);
  • Dependency management mechanism.

In traditional development, if we need to link databases, we need to load the PostgreSQL driver, we need to download it manually and put it in our classpath, if we need to export logs, we need to download log4j and put it in our classpath. This process is simple but time-consuming.

Maven makes it possible for projects of the same kind to have similar configurations, and makes it possible to automatically download/load development packages so that developers don’t have to spend a lot of time looking for dependencies, thus improving development efficiency.

1.1 Maven project structure

A clean Maven project directory structure looks like this:

Project | -- - SRC | -- - the main Java | -- - | -- - resources | -- - test Java | -- - | -- - pom. XMLCopy the code

Where pom. XML is the project description file, which defines the project attributes and all dependencies we can use. SRC /main/ Java stores the source code, SRC /main/resources stores the resource files, and SRC /test/ Java stores the test files. After a Maven project is built, Maven automatically identifies the directories.

1.2 the pom. XML

A simple pom.xml looks like this:


      
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
  <groupId>com.hs.gis</groupId>  <artifactId>MGIS</artifactId>  <version>1.0 the SNAPSHOT</version>  <dependencies>  <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.13</version>  <scope>test</scope>  </dependency>  </dependencies>  </project> Copy the code

GroupId is usually defined as the unique identifier of the project organization, corresponding to the Java package structure, artifactId is defined as the project name, and version is defined as the project version.

The dependencies tag defines all available dependencies. Each dependency is included in a dependency and uniquely identified by groupId, artifactId, and Version. Scope defines the scope of the dependency application.

Once declared, Maven automatically downloads the dependency package and places it on the classpath.

1.3 find the dependency

This site will tell you everything: https://mvnrepository.com/. Search for the dependencies you want to download in the search box and place them under the Dependencies TAB.

Create Maven project with IDEA

To open IDEA, click File-> New -> Project to create an empty project using Maven.

Directly to the next.

ArtifactId: Enter the project name.

GroupId: unique identifier of the project organization, corresponding to the Java package structure.

Once created you should have a project containing the following directories:

MGIS | -- - SRC | -- - the main Java | -- - | -- - resources | -- - test Java | -- - | -- - pom. XMLCopy the code

Support Web development

The project created above is not ready for Web development and requires the following steps:

3.1 Creating a WebApp Directory

MGIS | -- - SRC | -- - the main Java | -- - | -- - resources | -- - webapp | -- - test Java | -- - | -- - pom. XMLCopy the code

3.2 Configuring the Tomcat Server

  1. Open theFile->Project Structure, the newly builtFacets.

  2. Click Create Artifact to Create an Artifact.

  3. addTomcat local server.

And you’re done!

3.3 test

Create a new index.html file under WebApp.


      
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"  content="Width =device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">  <meta http-equiv="X-UA-Compatible" content="ie=edge">  <title>WebGIS</title> </head> <body> WebGIS </body> </html> Copy the code

Now the directory structure looks like this.

MGIS | -- - SRC | -- - the main Java | -- - | -- - resources | -- - webapp | -- - WEB - INF | -- - WEB. XML | -- - index. HTML | -- - test Java | -- - | -- - pom. XMLCopy the code

Start the Web server, wait a while, and you’ll see the WebGIS string in your browser.

At this point, we have set up the basic Web project.

Four source code

GitHub

conclusion

The above is the construction of the Web project, if you have any questions, welcome to leave a message with me.

Focus on

Welcome to the public account“Forest fungus”, mainly used to share programming experience and knowledge, to maintain depth and focus.