Build a Spring MVC Project step-by-step with Maven

Source: Internet
Author: User

1 Building a MAVEN Web project with Eclipse 1.1 creating a new MAVEN Web project

Open the menu file–new-mavenproject.

Click Next

Select the template type Archtype--maven-archtype-webapp. Then click Next.

Enter the group ID and artifact ID. The Group ID is typically populated with the project name, and the Artifact ID is typically populated with the name of the subproject.

The resulting project file structure is as follows:

Select the Pom.xml file and open the interface as shown below:

Add Properties: Expand the Properties option, then click Create ... Button as follows: Then the Name field is filled in with the Springversion,value field filled in 3.2.5.RELEASE. That is, an attribute springversion is added to the Pom.xml, and the property value is 3.2.5.RELEASE.

Select the Dependencies tab, open the Dependencies tab, and add a new dependency.

Group Id:org.springframework

Artifact Id:spring-web

Version:${springversion}

Click the OK button.

Description: The procedure is to join the Springframe spring-web dependent Library, ${springversion} is a previously set property.

New dependency:

Group Id:org.springframework

Artifact ID:SPRING-WEBMVC

Version:${springversion}

Click the OK button.

Description: The procedure is to join the Springframe SPRING-WEBMVC dependent Library, ${springversion} is a previously set property.

After the dependent library is set up, if the local does not exist also need to download the corresponding dependent library from the network, select the Pom.xml file, right click on the mouse to select Run As–maven install, and then the system automatically download the corresponding dependent library from the network.

After the dependent libraries have been downloaded, you can see the corresponding library files in the directory Javaresources–liraries–maven dependencies, as shown in:

Create a new folder in Java under the Src–main directory.

Create a new class Hello.java in Java. The package name is Com.springmvc.controller.

The contents of the Hello.java are as follows:

[Java]View PlainCopy 
  1. Package Com.springmvc.controller;
  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 Hello {
  7. @RequestMapping (value="/hello")
  8. Public String HelloWorld (model model) {
  9. Model.addattribute ("message","Hello world!!!");
  10. return "HelloWorld";
  11. }
  12. }

Create a new Folder view under the Src–main–webapp–web-inf directory and create a new file helloworld.jsp.

The contents of the helloworld.jsp file are as follows:

[HTML]View PlainCopy 
  1. <%@ page language="java" contenttype="text/html; Charset=iso-8859-1 "
  2. pageencoding="iso-8859-1"%>
  3. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <Meta http-equiv= "content-type" content= "text/html; Charset=iso-8859-1 ">
  7. <title>insert title here</title>
  8. </head>
  9. <body>
  10. <H1>message:${message}</h1>
  11. </body>
  12. </html>

With the Web. xml file selected, double-click to open the file, and modify the file so that it looks like this:

[HTML]View PlainCopy 
  1. <Web-app xmlns="Http://java.sun.com/xml/ns/javaee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "  
  4. version="3.0">
  5. <servlet>
  6. <servlet-name>spring-mvc</servlet-name>
  7. <servlet-class>org.springframework.web.servlet.dispatcherservlet</ Servlet-class>
  8. </servlet>
  9. <servlet-mapping>
  10. <servlet-name>spring-mvc</servlet-name>
  11. <url-pattern>/</url-pattern>
  12. </servlet-mapping>
  13. </Web-app>

In the Src–main–webapp–web-inf directory, create a new file Spring-mvc-servlet.xml with the following file contents:

[HTML]View PlainCopy 
  1. <beans xmlns="Http://www.springframework.org/schema/beans"
  2. xmlns:context="Http://www.springframework.org/schema/context"
  3. xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
  4. xsi:schemalocation= "
  5. Http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. Http://www.springframework.org/schema/context
  8. Http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. Http://www.springframework.org/schema/mvc
  10. Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
  11. <context:component-scan base-package="Com.springmvc.controller" />
  12. <Bean id="Viewresolver"
  13. class="Org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name= "prefix" value="/web-inf/view/" />
  15. <property name="suffix" value= ". jsp" />
  16. </Bean>
  17. </Beans>



Ok, all files have been built, now you can run the project, see how it works, select the item (click Com.liuht.springmvc, the top level of the project), click Run As–run on Server.

There is an interface for you to select the Web server you want to use, there are two options, one is an existing server, the other is to re-establish a new server, I choose the existing server, if you do not, you can reestablish a Web server.

Select the item you want to run, click the Add> button, add to the right of the selection box, if you have other unwanted items on the right, you can select and click the < remove button to delete. After the configuration is complete, click the Finish button.

See the following in the console window stating that the project started successfully:

Eclipse automatically opens its own browser and displays the following:

You can also open the browser input http://localhost:8080/com.liuht.springmvc/


This interface shows that the project has been successful, Hello world! This string of characters comes from the controller Hello.java file.

Build a Spring MVC Project step-by-step 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.