Eclipse Maven builds SPRINGMVC project

Source: Internet
Author: User
Tags maven central

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:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "2.4" xmlns= "http://java.sun.com/xml/ns/ Java EE "3 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "4 xsi:schemalocation=" http://java.sun.com/xml/ns/j2e E 5 Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> 6 7 <listener> 8 <listener-class>or G.springframework.web.context.contextloaderlistener</listener-class> 9 </listener>10 <servlet> ; <servlet-name>exam</servlet-name>13 <SERVLET-CLASS>ORG.SPRINGFRAMEWORK.WEB.SERVLET.D ispatcherservlet</servlet-class>14 </servlet>15 <servlet-mapping>17 <servlet-name& gt;exam</servlet-name>18 <url-pattern>/</url-pattern>19 </servlet-mapping>20 &L t;welcome-file-list>22 <welcome-file>index.jsp</welcome-file>23 </welcome-file-list>24 &L T;/web-app> 

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:

1 <?xml version= "1.0" encoding= "UTF-8"? >2 <! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >3 <beans >4 </beans>

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:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <!--Bean Head--3 <beans xmlns= "http://www.springframework.org /schema/beans "4 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:p=" http://www.springframework.org/ schema/p "5 xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context=" http://www.springframework.org/ Schema/context "6 xmlns:util=" Http://www.springframework.org/schema/util "7 xsi:schemalocation=" HTTP://WWW.SPRINGF Ramework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 Http://www.spri Ngframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 9 http:/             /WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 10 Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!--activating @controller mode-->13    <mvc:annotation-driven/>14 <!--scanning all classes in the package to complete the function of bean creation and automatic dependency injection needs to be changed-->15 <context:component- Scan base-package= "Cc.monggo.web.controller"/>16 <bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/>18 <bean id=" Viewresolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver ">20 <property name=" Prefix ">21 <value>/web-inf/jsp/</value>22 </property>23 <property Name = "suffix" >24 <value>.jsp</value>25 </property>26 </bean>27 &LT;/BEANS&G   T

 

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:

 1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 <groupId>exam</groupId> 5 <artifactid>exam_3</ Artifactid> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 &LT;NAME&G T;exam_3 Maven webapp</name> 9 <url>http://maven.apache.org</url>10 <dependencies>11 <d Ependency>12 <groupid>junit</groupid>13 <artifactid>junit</artifactid>14 < version>3.8.1</version>15 <scope>test</scope>16 </dependency>17 <depend ency>19 <groupid>org.springframework</groupid>20 <artifactid>spring-web</artifacti D>21 <version>3.0.5.release</version>22 </dependency>23 <dependency>25 <groupid>org.springframework</group Id>26 <artifactid>spring-webmvc</artifactid>27 <version>3.0.5.RELEASE</version> </dependency>29 <dependency>31 <groupid>org.apache.geronimo.specs</groupid& Gt;32 <artifactid>geronimo-servlet_2.5_spec</artifactid>33 <version>1.2</version>3 4 </dependency>35 </dependencies>37 <build>38 <finalname>exam_3</fin alname>39 </build>40 </project>

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:

1 package Cc.monggo.web.controller; 2  3  4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6  7 Import Org.springframework.stereotype.Controller; 8 Import org.springframework.web.bind.annotation.RequestMapping; 9 Import org.springframework.web.servlet.modelandview;10 Import cc.monggo.domain.loginform;12 @Controller14 public class Logincontroller {     @RequestMapping (value= "login")     Modelandview Login ( HttpServletRequest request,httpservletresponse response,loginform command) {         String username = Command.getusername ()         modelandview mv = new Modelandview ("/index/index", "command", "LOGIN SUCCESS," + username) ;         return mv;20     }21}
1 package cc.monggo.domain; 2  3  4 public class LoginForm {5     private string username; 6     private string password; 7 public     string g Etusername () {8         return username; 9     }10 public     void Setusername (String username) {One         this.username = username;12     }13 public     String GetPassword () {+         return password;15     }16 public     Void SetPassword (String password) {         This.password = password;18     }19}

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

1 <%2     request.getrequestdispatcher ("/web-inf/jsp/login/login.jsp"). Forward (request,response); 3%>

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

1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2     pageencoding=" Utf-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2     pageencoding=" Utf-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 

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:

  

Eclipse Maven builds SPRINGMVC project

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.