Build MAVEN's SPRINGMVC project with Eclipse

Source: Internet
Author: User
Tags sonatype

Reprint: http://limingnihao.iteye.com/blog/830409

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

Installing the Maven plugin with MyEclipse has some problems with the MAVEN project that was built. First, the resources will never be released to Tomcat when releasing Tomcat, and the second is to change the classes under Web-inf to the classes under target, But I don't know why MyEclipse is still generating class under Web-inf. It's either true that the web-inf generated classes, but when you publish Tomcat, the class file will not be published in the past, super depressed. But after using eclipse to build a MAVEN project, it's no problem to open it with MyEclipse.

The benefits of using Maven: I feel the most important thing is to automatically download the jar package and the package it relies on, which ensures that the jar version is different when many people develop. And then the file structure is clear, Java files, resource files, test files are clearly divided.

Two methods will be introduced: first, to build the Maven project method directly, and to build the dynamic Web project into Maven project method.

First, the direct establishment of MAVEN project Method 1, the establishment of the MAVEN project

Next, use Eclipse's maven to build a Web project to build the SPRINGMVC project as an example:

1.1 Choosing to build Maven Project

Select File, new, and other, in the new window, select Maven-and Maven Project. Click Newxt.

1.2 Select project Path

Use the default Workspace location defaults workspace.

1.3 Select project Type

Select Maven-archetype-webapp in the Artifact ID

1.4 Enter the group ID and Artifact ID, and the package

Group IDs generally write large project names. The Artifact ID is the child project name.

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

Package is the default for you to build a bag, do not write or can.

1.5 The file structure of the newly created

If there is more content displayed here, it is generally a problem with the filters setting. or perspective for Java EE mode, it is possible to change it into a JavaScript mode.

2. Configuring MAVEN Projects

The next step is to change a lot of configurations.

2.1 Adding the source folder

Next you need to add a Src/main/java,src/test/java, src/test/resources three folders. Right-click on the project root to New, Source Folder,

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

2.2 Changing the class path

Right-click 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, choose target/test-classes;

Select the Allow output folders for source folders.

You'll also change it here:

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

Change 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-click the project, select Project Facets, tap Convert to faceted from

2.3.2 Configuring Project Facets

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

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

2.3.3 Configuring Modify Faceted Project

Click further configuration available ..., pop-up 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 generates the Web. xml file, optional.

2.4 Setting up Deployment Assemblies (Web Deployment Assembly)

After the above steps are set up, clicking the Ok,properties window will close and open this window in the right-click Project. A deployment Assembly appears in the list on the left, click to enter, such as:

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

1, we delete the two items of test, because tests are used for testing and do not need to be deployed.

2, set the Maven jar package to be published under Lib.

ADD, Java Build Path Entries, Maven Dependencies, Finish

Setup Complete

3. Add a jar package to the MAVEN project

Maven can manage the jar packages that the project relies on, with GroupID, Artifactid, and version numbers to uniquely identify a jar package. This prevents the web-inf/lib of the jar package from being inconsistent under the old Web project. Maven also automatically downloads the jar packages that are added to the jar package.

3.1 Add the required jar packages to the Pom.xml

Using Maven POM Editor to open the Pom.xml file in the project, select Dependencies, click Add in the Dependencies column, first pop up a search button, such as input spring-web, will automatically search for spring-web related jar packages, 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 copied directly to dependencies content.

3.2 Setting the scope of the JAR package

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

    1. Compile, the default value, applies to all stages 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, used only at runtime, such as the JDBC driver, for the run and test phases.
    4. Test, used only during testing, to compile and run the test code. Will not be published with the project.
    5. System, similar to provided, needs to explicitly provide an jar,maven that contains dependencies and does not look for it in repository.

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

Sometimes found that Servlet-api is still packaged under the Lib, at this time will definitely error. You will 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. Build SPRINGMVC Framework

4.1 Editing the Web. xml file

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

The Webx.xml code is as follows:

XML code
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <web-app xmlns= "Http://java.sun.com/xml/ns/javaee"
  3. Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
  5. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
  6. version= "2.5" >
  7. <!--the project name to prevent default names---
  8. <context-param>
  9. <param-name>webAppRootKey</param-name>
  10. <param-value>maven.example.root</param-value>
  11. </context-param>
  12. <!--spring log4j listener--
  13. <listener>
  14. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  15. </listener>
  16. <!--character Set filter--
  17. <filter>
  18. <filter-name>CharacterEncodingFilter</filter-name>
  19. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  20. <init-param>
  21. <param-name>encoding</param-name>
  22. <param-value>UTF-8</param-value>
  23. </init-param>
  24. <init-param>
  25. <param-name>forceEncoding</param-name>
  26. <param-value>true</param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>CharacterEncodingFilter</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33. <!--Spring View Distributor-
  34. <servlet>
  35. <servlet-name>dispatcher</servlet-name>
  36. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  37. <init-param>
  38. <param-name>contextConfigLocation</param-name>
  39. <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  40. </init-param>
  41. <load-on-startup>1</load-on-startup>
  42. </servlet>
  43. <servlet-mapping>
  44. <servlet-name>dispatcher</servlet-name>
  45. <url-pattern>*.do</url-pattern>
  46. </servlet-mapping>
  47. </web-app>

4.2 Writing the spring configuration file Dispatcher-servlet.xml

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

XML code
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <beans xmlns= "Http://www.springframework.org/schema/beans"
  3. xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
  4. xmlns:context= "Http://www.springframework.org/schema/context"
  5. Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
  6. xmlns:tx= "Http://www.springframework.org/schema/tx"
  7. Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
  8. Xsi:schemalocation= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
  9. Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. Http://www.springframework.org/schema/beans
  11. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. Http://www.springframework.org/schema/context
  13. Http://www.springframework.org/schema/context/spring-context-3.0.xsd
  14. Http://www.springframework.org/schema/mvc
  15. Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  16. Http://www.springframework.org/schema/tx
  17. Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
  18. <mvc:annotation-driven/>
  19. <context:component-scan base-package= "Liming.maven.example"/>
  20. <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
  21. <property name= "prefix" value= "/web-inf/views/"/>
  22. <property name= "suffix" value= ". jsp"/>
  23. </bean>
  24. </beans>

4.3 Writing a controller layer test class

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

Java code
    1. Package Liming.maven.example.view;
    2. Import Org.springframework.stereotype.Controller;
    3. Import Org.springframework.ui.Model;
    4. Import org.springframework.web.bind.annotation.RequestMapping;
    5. @Controller
    6. public class Generalcontroller {
    7. @RequestMapping (value= "index.do")
    8. public void index_jsp (model model) {
    9. Model.addattribute ("Liming", "Dawn Hello");
    10. System.out.println ("index.jsp");
    11. }
    12. }

4.4 Writing index.jsp pages

First, create a folder 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 file under views

We use Jstl to get Controlleradd data.

The JSP page code is as follows:

HTML code
  1. <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>
  2. <%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
  3. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
  4. <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
  5. <title>insert title here</title>
  6. <body>
  7. <c:out value= "${liming}" ></c:out>
  8. </body>

5, publish to Tomcat

There's nothing to say about this.

Eclipse under Tomcat common settings: http://limingnihao.iteye.com/admin/blogs/825394

6, Test

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

The results of the visit come in one of:

II. build Dynamic Web project into Maven project Method 7, the second method Dynamicweb project to Mavan7.1 new Dynamic Web project

Select Select File, New, and other. In the new window, select Dynamic Web Project under the Web. Click Next.

7.2 Configuring project Information 7.2.1 Entering project information

First enter the project name.

Target runtime generally chooses what containers to use, such as Tomcat. You can not select it first.

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

Configuration is easy to choose. If the target runtime chooses Tomcat. Here corresponds is also the selection tomcat.

Next.

7.2.2 Configuring the folder for source and output

This window enters the source folders that needs to be established. and the default output folder.

This our source folders is added after the project is completed.

The output folder can be changed to target/classes.

7.2.3 Configuring the path to the Web. xml file

7.3 Turn into MAVEN projects

Once the Dynamic Web project is established, first turn it into a MAVEN project.

Right-click this project and select Maven---Enable Dependency Management. Pop-up window, direct finish is available.

7.4 Other configurations

The next configuration is as basic as the previous one.

Please refer to the above.

2.1 Adding the source folder

2.2 Change the class path.

2.3.3 Configuring Modify Faceted Project

2.4 Setting up Deployment Assemblies (Web Deployment Assembly)

Then, build the SPRINGMVC framework, publish it to Tomcat, and test it.

Build MAVEN's SPRINGMVC project with Eclipse

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.