Java EE Learning (5): Idea + maven + Spring Build Web (1)

Source: Internet
Author: User

Reference:http://www.cnblogs.com/lonelyxmas/p/5397422.html

  Http://www.ctolib.com/docs-IntelliJ-IDEA-c--159047.html

Hole Teacher's "springmvc video tutorial "

Record: This section is mainly completed using the MAVEN Management Spring + Project package to build the Maven+spring Web project platform.

Prerequisite: Installed and configured properly

-Intellij Idea 16.3.5 Ultimate

-JDK 1.8.0_45

-Maven 3.3.9

-Tomcat 8.5.12

-MYSQL 5.7

-Spring 4.0.0

-Bootstrap 3.3.5 (optional)

Not clear to refer to

1. Create a Maven project

For developers who don't use Maven, you can build a simple Web project directly. If you use Maven, follow the diagram.

(1) File->new Project can enter the following interface: First select the left column maven, and then configure the JDK (generally if you have added a JDK before, it will be automatically populated, if not added, click Next to the New JDK directory to import). Tick "Create from archetype", then select the 4 blue position webapp, click Next.

(2) Next to enter the following interface: Here you need to fill in GroupID and Artifactid and version, these three properties are intended to identify the uniqueness of your project, For example, Tomcat's GroupID is Org.apache, which is the Apache organization project, Artifactid is Tomcat, the project name is Tomcat, and I am currently using version 7.0.68. These are only available at the time of release, so you can fill in the blanks and fill out the next point.

(3) Next, go to the following interface (configure MAVEN): Open Maven home directory, you can see that IntelliJ idea has integrated maven 2 and MAVEN 32 versions, if using the default integrated MAVEN, Select Buldled (Maven 3) and click Next directly.

We can also import the newer MAVEN version of the new installation locally, click on the blue arrow to the right ... button to import the MAVEN path, click Next:

(4) Next after entering the following interface: Fill in the project name (can be the same as the above ID, also can be different), select the Project save path, click Finish:

(5) After finish, go to the following interface, MAVEN will build the Web project in the background, which will take a certain amount of time, depending on the network environment, experience found that the newer version of the MAVEN project generated faster, using idea integrated maven may wait for a long time practice.

The file structure of the project is shown in the red box on the left. As you can see, it creates the following folders under Src/main:

recources folder: Generally used to store some resource files WebApp folder: Used to store Web configuration files and JSP pages, etc., which has formed a raw web application.

Note: Select the enable-auto-importof the red box on the right , you can automatically download and import the jar package after each modification of pom.xml, which is detailed later.

2. Create a new source folder and name it Java, and set:

(1) Press ctrl+alt+shift+s, or File->project Structure, or click below: View project structure

Appear as:

(2) Select Modules, create a new folder in the Springmvcdemo Src\main folder, named Java:

Next, check the Java folder, click on the Make As:sources, the folder will be blue, to save Java code, press OK, end the configuration.

3.maven Auto-import jar package (Add Project package): Include Spring package (using Spring Framework), test package, log package, etc. ..

Since we are going to develop with SPRINGMVC, there is no doubt about the Springmvc jar package. If you do not use MAVEN, then you need to go to the official website to download the relevant jar package, and then import into the project. Now with Maven, there's no need to surf the web for Jar packs. Specifically, I'm going to be one by one.

Maven does a very simple job of automatically downloading the jar packages you need to be locally and then associating them with the project. All of Maven's jar packages are stored in a few central warehouses, and one of the most commonly used is maven Repository, which is what jar package you need, and it will get you from the warehouse. So how do you tell maven what jar packages are needed? Let's look at the engineering catalog, find a Pom.xml file (which is already in front of you when you've just created a project), and Maven is relying on it to define the requirements, the code is as follows:

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>testWeb</artifactId> &lt ;p ackaging>war</packaging> <version>1.0-snapshot</version> <name>testweb Maven webapp</name> <url>http://maven.apache.org</url><dependencies> <dependency> <groupId>junit</groupId> &LT;ARTIFACTID&G T;junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <bui Ld> <finalName>testWeb</finalName> </build></project>

We can see that this file contains information such as the gropid of the project we defined earlier, which is the identity of the project and we do not want to change it.

Focus on <dependencies> tags , translation is the meaning of "dependence", that is, the requirements of each package is called a dependent <depedency>, defined in <dependencies>. In each <depedency>, you need to provide the three necessary information for the GroupID, Artifactid, and version of the required jar package. For example above we see the introduction of a JUnit package, in the following format:

<dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>3.8. 1</version>    <scope>test</scope></dependency>

This is a unit test package that provides three basic information, and a 4th scope is not required for other packages. All jar packages are introduced in this format. So how to look at the 3 of these jar package information, perhaps the new contact is not very familiar with the developer, this time need to consult the warehouse. For example, we need to introduce the Spring core jar package Spring-core, open Maven Repository, search for spring-core, and enter the following interface:

Click into the Red box selected Spring Core, as shown below, you can see the usage of each version:

Select Use up to version 4.3.7.RELEASE, you can see its dependency as shown in the following red box:

We copy it to the <dependencies> in pom.xml (This section uses the directory in the reference instance, does not affect, directly uses its own corresponding directory file, does not need to notice what):

In this way, Maven will begin to download the jar package automatically to the local repository and then link it to your project, and after the download is complete, we expand the External Libraries in the project catalog:

It can be found that although we only write a dependency, it imports two jar packages, that is, when importing a jar package, the jar package that is closely related to it is also imported.

In addition to Spring-core, I also want to Spring-context, copy Spring-core <dependency>, Spring-core to Spring-context, as follows:

<dependency>    <groupId>org.springframework</groupId>    <artifactid>spring-context </artifactId>    <version>4.3.  7. Release</version></dependency>

After the download is complete, see external Libraries, will not find, an instant import a lot of jar package (of course not an instant, it depends on your speed) it:

This is the strength of Maven, and if you need to use SPRINGMVC to develop a website, simply remember the name of a few important packages and you can easily import all the packages into the project.

To make a long story short, now we are going to develop SPRINGMVC, please turn your pom.xml into the following, of course, do not change your grupid and other information (from modelversion to the URL do not move):

<properties>        <spring.version>4.3.  7. release</spring.version>        5.2.  . Final

Please include the following dependencies in <dependencies>:

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency>            <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <gro Upid>org.springframework</groupid> <artifactId>spring-context</artifactId> < version>${spring.version}</version> </dependency> <dependency> &LT;GROUPID&G T;org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version >${spring.version}</version> </dependency> <dependency> <groupid>org.s Pringframework.data</groupid> <artifactId>spring-data-jpa</artifactId> <version >1.11.1. release</version> </dependency> <dependency> <groupid>org.hibernate</g Roupid> <artifactId>hibernate-entitymanager</artifactId> &LT;VERSION&GT;${HIBERNATE.V ersion}</version> </dependency> <dependency> <groupid>org.hibernate</g Roupid> <artifactId>hibernate-c3p0</artifactId> &LT;VERSION&GT;${HIBERNATE.VERSION}&L            t;/version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupid>javax.servlet</groupid&            Gt <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1. the</version> </dependency>

Change <build> to the following form:

<build>     <finalName>springmvcdemo</finalName>     <plugins>          <plugin>               <groupId>org.apache.maven.plugins</groupId>               <artifactid>maven-compiler-plugin</ artifactid>               <configuration>                   <source>1.8</source>   # 1.8 Here refers to the JDK The current version is typically 1.7 or 1.8                   <target>1.8</target>               </configuration>          </ Plugin>     </plugins></build>

My complete Pom.xml file is as follows:

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>testWeb</artifactId> <packaging>war</packaging> <version>1.0-snapshot</version> <name>testweb Maven webapp</name> <url>http://maven.apache.org</url><properties> <spring.version>4.3.7. Release</spring.version> 5.2.Ten. final3.8.1</version> <scope>test</scope> </dependency> <dependency>            <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <gro Upid>org.springframework</groupid> <artifactId>spring-context</artifactId> < version>${spring.version}</version> </dependency> <dependency> &LT;GROUPID&G T;org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version >${spring.version}</version> </dependency> <dependency> <groupid>org.s Pringframework.data</groupid> <artifactId>spring-data-jpa</artifactId> <version >1.11.1. release</version> </dependency> <dependency> <groupid>org.hibernate</g Roupid> <artifactId>hibernate-entitymanager</artifactId> &LT;VERSION&GT;${HIBERNATE.V ersion}</version> </dependency> <dependency> <groupid>org.hibernate</g Roupid> <artifactId>hibernate-c3p0</artifactId> &LT;VERSION&GT;${HIBERNATE.VERSION}&L            t;/version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupid>javax.servlet</groupid&            Gt <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1. the</version> </dependency> </dependencies> <build> <finalname>testweb</ finalname> <plugins> <plugin> <groupid>org.apache.maven.plugins</                     Groupid> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
Complete Pom.xml file

We can see that, in addition to importing spring-related jar packages, there are some other packages that are useful, and we'll talk about them later. If you do not use MAVEN, download the relevant jar packages such as spring, Hibernate, MySQL, JSTL, etc. and import into the project yourself. At this point, the import of the jar package is complete

Add Maven Quick Troubleshooting Dependency pack conflicts here

(1) For example, click the arrow button ,

    • The project dependency structure diagram appears on the left, which allows us to observe the dependencies of the project in good condition.
    • As shown in callout 1 below, we can edit the dependency directly on the dependency structure diagram, which is commonly used for this exclusion function.

(2) as described, generally we appear in the red line is to be excluded, to prevent the emergence of a different version of the dependency package caused by the failure to compile.

(See also Java EE Learning (6): Idea + maven + Spring Build Web (2)-Configure Spring)

Java EE Learning (5): Idea + maven + Spring Build Web (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.