Eclipse uses MAVEN to build a SPRINGMVC project _java

Source: Internet
Author: User
Tags aop zip sonatype log4j

First eclipse needs to install Maven's plugin, address: http://m2eclipse.sonatype.org/sites/m2e.

Installing the Maven plugin with MyEclipse, there are some problems with the MAVEN project built. One is that resources are never released to Tomcat when it is released, and the second is to change the classes under Web-inf to the classes under target, But don't know why myeclipse either still generates class under Web-inf. or really not in the web-inf generated classes, but the release of Tomcat, class files will not give you the past, super depressed. But after using eclipse to build the MAVEN project, it's no problem to use the MyEclipse open.

The benefits of Maven: I feel that the most important thing is to automatically download the jar package and the packages it relies on to ensure that the jar version is different for many people when they develop. Then the file structure is clear, Java files, resource files, test files are divided very clearly.

Two approaches will be introduced: one, directly building the MAVEN project approach, and building a dynamic Web project into a Maven project method.

First, the direct establishment Maven project method

1. Establish MAVEN Project

Next, use the eclipse maven to build a Web project to build the SPRINGMVC project as an example:

1.1 Select Create Maven Project

Select File-> new-> other to select Maven-> maven Project in the new window. Click Next.

1.2 Select project Path

Use default Workspace location workspace.

1.3 Select project Type

Select Maven-archetype-webapp in artifact ID

1.4 Enter the group ID and artifact ID, as well as the package

The Group ID typically writes a large project name. The artifact ID is the child project name.

For example, Spring's Web package, Group id:org.springframework,artifactid:spring-web.

Package is the default for you to build a package, do not write can also.

1.5 Just after the establishment of the file structure as shown below

If the content shown here is more, it is generally the problem of filters setting. Or perspective is Java EE mode, it is OK to change it to java-based mode.

2. Configure MAVEN Project

Then you need to change a lot of configurations.

2.1 Add source Folder

Next you need to add Src/main/java,src/test/java, src/test/resources three folders. Right key item root directory Click New-> Source Folder,

Build these three folders. Note that you are not building a common folder, but source folder.

2.2 Changing the class path

Right-key project, Java build Path-> Source

There should be 4 folders below. Src/main/java,src/main/resources,src/test/java, Src/test/resources.

Double-click the output folder for each of the folders to select the path.

Src/main/java,src/main/resources, choose target/classes;

Src/test/java, src/test/resources, select Target/test-classes;

Select the Allow output folders for source folders.

Here you will also change:

Change the order in which folders are displayed: Click Orders and Export.

To change the JDK version: in libraries, double-click the JRE System Library to version 1.6.

2.3 Turning the project into a Dynamic Web project

2.3.1 Right key item, select Project Facets, click Convert to faceted from

2.3.2 Configuration Project Facets

Change the version of Dynamic Web module to 2.5. (3.0 for Java7).

If you are prompted for an error, you may need to set the compiler compliance level to 1.6 in the Java compiler. Or you need to change the Java version of this window to 1.6.

2.3.3 Configuration Modify Faceted Project

Click further configuration available ..., eject modify faceted Project window

Here is the path to set the Web.xml file, we enter Src/main/webapp.

Generate Web.xml Deployment descriptor automatically generate Web.xml files, optional.

2.4 Setting up the deployment assembly (WEB deployment Assembly)

When the above steps are set, clicking the Ok,properties window closes and opens the window in the right key item. A deployment Assembly appears in the list on the left, and when clicked, the following figure:

The list here is the path to the file publication when the project is deployed.

1. We delete the two items of test because test is used for testing and does not require deployment.

2. Set up the Maven jar package to be published under Lib.

ADD-> Java Build Path Entries-> Maven dependencies-> Finish

To set the finish chart

3. Add a jar package to the MAVEN project

Maven can manage the jar packages that the project relies on, with the GroupID, Artifactid, and version numbers uniquely able to determine a jar package. This prevents the web-inf/lib of jar packs from being used in older Web projects. and maven automatically downloads the jar packages that the jar packages that were added to depend on.

3.1 Add the required jar package to the Pom.xml

Using Maven POM Editor to open the Pom.xml file in your project, select dependencies, click Add on the Dependencies column, and first pop up a search button, such as input spring-web, Automatically searches for spring-web-related jar packs, we select the 3.0.5 version of spring. Add the spring package all in. Additional jar packages that need to be added are: JUnit, Jstl. or click Pom.xml to edit the Pom.xml file directly. This can be directly copy over dependencies content.

3.2 Set scope for JAR packs

When adding into a jar package, there are some attributes that need to be set, most importantly scope, which has the following values:

1.compile, the default, applies to all phases and is published along with the project.

2.provided, similar to compile, expects the JDK, container, or user to provide this dependency. such as Servlet.jar.

3.runtime, only used at run time, such as JDBC driver, applicable to run and test phase.

4.test, used only during testing, for compiling and running test code. Will not be published with the project.

5.system, similar to provided, need to explicitly provide jar,maven that contains dependencies and will not find it in repository.

Typically, the SPRINGMVC project needs to configure the scope of the jar package as shown below:

Sometimes found Servlet-api or was packaged under the Lib, at this time will certainly be an error. You'll need to install the WTP in the Maven plugin as well.

Eclipse Online Installation path: Http://m2eclipse.sonatype.org/sites/m2e-extras. Select for Eclipse WTP.


4. Constructing SPRINGMVC Framework

4.1 Editing Web.xml files

Need to add log4j, character filtering, Spring dispatcher, and so on.

The Webx.xml code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com /xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 > <!--differentiate project names against default duplicate--> <context-param> <param -name>webapprootkey</param-name> <param-value>maven.example.root</param-value> </ Context-param> <!--Spring's log4j listener--> <listener> <listener-class>org.springframework.web.ut Il. Log4jconfiglistener</listener-class> </listener> <!--character Set filter--> <filter> <filter-na Me>characterencodingfilter</filter-name> <filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name >encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-valu e> </init-param> </filter> <filter-mapping> <filter-name>characterencodingfilter</fi Lter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Spring View distributor--> &L T;servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> 
 Contextconfiglocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> < Servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping > </web-app>

4.2 Writing the spring configuration file Dispatcher-servlet.xml

To add MVC driver, annotation detection, view resolution, and so on. The Dispatcher-servlet.xml code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: 
 aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns: 
 Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/beans http:// Www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http:// Www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http:// Www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http:// Www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <mvc:annotation-driven/> <context:component-scan base-package= "liming.maven.example"/> <bean "class=". Servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf/views/"/> < Property name= "suffix" value= ". jsp"/> </bean> </beans>

4.3 Write a controller layer test class

Write a SPRINGMVC controller layer test class. This class has only one method to do address mapping and pass a data to the page. The code is as follows:

Package Liming.maven.example.view; 
 
Import Org.springframework.stereotype.Controller; 
Import Org.springframework.ui.Model; 
Import org.springframework.web.bind.annotation.RequestMapping; 
 
@Controller public 
class Generalcontroller { 
 
 @RequestMapping (value= "index.do") is public 
 void index_jsp ( Model model) { 
 Model.addattribute ("Liming", "Dawn Hello"); 
 System.out.println ("index.jsp"); 
 } 

4.4 Writing index.jsp Page

First, the folder views are built under Src/main/webapp/web-inf. This is the same as the prefix property path in the Dispatcher-servlet.xml configuration file.

Build index.jsp files under views

We use Jstl to get Controlleradd data.

The JSP page code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix=" 
C "uri=" Http://java.sun.com/jsp/jstl/core "%> 
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 
 
 

5 . Publish to Tomcat

There's nothing to say about this.

6 . Test

Access Address: http://localhost:8080/liming.maven.example/index.do

The results of the visit to a screenshot:

Second, build dynamic Web project into Maven project method
7, the second method of Dynamicweb project into Mavan

7.1 New Dynamic Web Project

Select File-> New-> other in turn. In the new window, select Dynamic Web Project under the Web. Click Next.

7.2 Configuring project Information
7.2.1 Enter project information

Enter the project name first.

Target runtime typically chooses what containers to use, Tomcat, and the like. You may not choose it first.

Dynamic Web module version. is the version number. Option 2.5 (3.0 is Java7).

Configuration is an easy configuration to choose from. If the target runtime chooses Tomcat. Here corresponds is also the choice tomcat.

Next.

7.2.2 Configure the source and output folder

This window enters the source folders that you want to build. and default output folder.

This our source folders and so on is added after the project has been built.

Output folder can be conveniently changed into target/classes.


7.2.3 Configure the path to the Web.xml file

7.3 Convert to Maven project

After the Dynamic Web project is established, it is first converted to a MAVEN project.

Right-click this item and select Maven-> Enable Dependency Management. The pop-up window, the direct finish is OK.

7.4 Other configurations

The next configuration work is the same as the previous one.

Please refer to the above.

2.1 Add source Folder

2.2 Change the class path.

2.3.3 Configuration Modify Faceted Project

2.4 Setting up the deployment assembly (WEB deployment Assembly)

The next is to build the SPRINGMVC framework, publish it to Tomcat, test it.

Attachment Download:

Liming.maven.example.zip

Liming.dynamic.example.zip

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.