Eclipse creates a Web project with Maven

Source: Internet
Author: User
Tags maven central

Eclipse builds Web projects with Maven (2013-01-27 11:05:31) reproduced
Tags: it Eclipse maven spring web Talk

I. BACKGROUND information

For starters, building a project with MAVEN is not an easy thing, SPRINGMVC is not the mainstream of MVC, but I think springmvc than struts to use, really do 0 configuration. Once used, it will not fit.

Two. Preparatory work

1.Eclipse 3.7

2.maven

3.Eclipse requires the Maven plugin to be installed. Url:maven-http://download.eclipse.org/technology/m2e/releases. Install maven-3.0.4. and select local maven, such as:

  

Three. Building the project

1. Build the project framework with the MAVEN plugin

MAVEN has powerful build capabilities, and using MAVEN can build many different types of projects. Here we build Maven-archetype-webapp types of projects. Select Other in Eclipse->new to find the MAVEN project type. Such as:

  

After selecting the path, we select the build type, such as:

  

Next, fill in the project's group Id,artifact Id, such as:

  

The group ID here is the id,arifact ID of the large item. Like a large project, there are many small projects that make up the same. At this point, our project has been shaped to look like:

  

Next, we want to refine the project's directory, configuration.

2. Perfecting the project

First, complete the directory, add the important source folder, this is not a simple floder, these folders will be involved in the compilation. Add Src/main/java,src/test/resources,src/test/java directory. Make the catalog into a standard MAVEN structure. Such as:

  

Next, change some of the configurations:

Let the JDK of the project use the local JDK;

Let the character set of the project be UTF-8;

Change the catalog order of the project;

After all this is done, the project catalog should look like this:

  

  

3. Turn the project into a Web project

At this point, our project is not a standard Web project, you can add the features of Web Engineering in Eclipse, select the properties of the project, choose Project Facets, such as:

  

Here, we choose the dynamic Web Module, version Select 2.4, this version is more generic. Such as:

  

At this point, we see that there is one more WebContent directory in the directory, because the Web directory is Src/main/webapp with Maven build, so we delete the WebContent directory. Next, to configure the Web project's publish directory, is deployment Assembly,

  

The test directory is not published, the WebContent directory is missing, so the three items are deleted. and add Src/main/webapp directories, and Maven Dependenices, after completing such as:

  

So, our project is completely a Web project.

4. SPRINGMVC characteristics of the given project

Configure Web. XML to have SPRINGMVC features, two main configurations, one Contextloaderlistener and one dispatcherservlet. The code is as follows:

Xml

Configuration Contextloaderlistener indicates that the project is to be started in spring mode. At startup, the Applicationcontext.xml as the Spring container's configuration file is found by default in the/web-inf directory, where some beans, such as DataSource, can be initialized. We don't do anything here. The code is as follows:

Applicationcontext.xml

Configuration Dispatcherservlet says the project will be in SPRINGMVC manner. Startup will also default in the/web-inf directory to find Xxx-servlet.xml as a configuration file, xxx is the name of Dispatcherservlet, the file will be configured with two important MVC features:

Handlermapping, is responsible for dispatcherservlet this front-end controller request to find controller;

Viewresolver, which is responsible for finding the Modelandview view parser for Dispatcherservlet.

The code is as follows:

Exam-servlet.xml

 

5. Let maven automatically configure the jar package

When you build the framework with MAVEN, Pop.xml is generated, which is the MAVEN configuration file. We want to introduce a package of Spring-web,servlet and other features. The code is as follows:

Pop.xml

MAVEN is so simple that once saved, Maven will automatically download the Pop.xml jar package. You can now see that the jar package is generated under MAVEN dependencies in the directory.

More jar packages can be downloaded from the MAVEN Central Library: http://mvnrepository.com.

  

6. Do a test

Said a lot of, only the fun to run, write a simple test below. Write the controller first. Write two classes, Logincontroler.java,loginform.java. The code is as follows:

LoginController.javaLoginForm.java

Add some JSP, home page of index.jsp, mainly do jump, code as follows:

index.jsp

There are two JSP, do some simple function, a form login.jsp, a form submitted return index.jsp, the code is as follows:

login.jspindex.jsp

The directory structure for the entire project is as follows:

  

Run it in Tomcat. Do not use the Tomcat plugin, there may be a problem. Just in a normal way. Operating effects such as:

  

The article comes from:

Http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html

Eclipse creates a Web project with Maven

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.