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
- Package Com.springmvc.controller;
- Import Org.springframework.stereotype.Controller;
- Import Org.springframework.ui.Model;
- Import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- Public class Hello {
- @RequestMapping (value="/hello")
- Public String HelloWorld (model model) {
- Model.addattribute ("message","Hello world!!!");
- return "HelloWorld";
- }
- }
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
- <%@ page language="java" contenttype="text/html; Charset=iso-8859-1 "
- pageencoding="iso-8859-1"%>
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=iso-8859-1 ">
- <title>insert title here</title>
- </head>
- <body>
- <H1>message:${message}</h1>
- </body>
- </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
- <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_3_0.xsd "
- version="3.0">
- <servlet>
- <servlet-name>spring-mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.dispatcherservlet</ Servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring-mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </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
- <beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:context="Http://www.springframework.org/schema/context"
- xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
- xsi:schemalocation= "
- 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 ">
- <context:component-scan base-package="Com.springmvc.controller" />
- <Bean id="Viewresolver"
- class="Org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name= "prefix" value="/web-inf/view/" />
- <property name="suffix" value= ". jsp" />
- </Bean>
- </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