使用maven一步一步构建spring mvc项目


1      使用eclipse构建maven web项目

1.1新建Maven的web项目

打开菜单File –New-MavenProject。

maven(1):使用 eclipse 构建maven web项目_xml

点击Next

maven(1):使用 eclipse 构建maven web项目_mvc_02

 

 

选择模板类型archtype——maven-archtype-webapp。然后点击Next。

maven(1):使用 eclipse 构建maven web项目_mvc_03

 

输入Group Id和artifact Id。Group Id一般填入项目名称,Artifact Id一般填入子项目的名称。

maven(1):使用 eclipse 构建maven web项目_mvc_04

 

生成的项目文件结构如下所示:

maven(1):使用 eclipse 构建maven web项目_mvc_05

 

选择pom.xml文件,并打开,界面如下所示:

maven(1):使用 eclipse 构建maven web项目_mvc_06

 

增加Properties:展开Properties选项,然后点击Create…按钮,如下所示:然后Name字段填入springVersion,Value字段填入3.2.5.RELEASE。即在pom.xml中增加了一个属性springVersion,属性值为3.2.5.RELEASE。

maven(1):使用 eclipse 构建maven web项目_xml_07

 

选择Dependencies标签,打开Dependencies选项卡,并增加一个新的Dependency。

maven(1):使用 eclipse 构建maven web项目_xml_08

maven(1):使用 eclipse 构建maven web项目_spring_09

 

 

maven(1):使用 eclipse 构建maven web项目_mvc_10

Group Id:org.springframework

Artifact Id:​​spring​​-web

Version:${springVersion}

点击ok按钮。

说明:该过程是加入springframe的spring-web依赖库,${springVersion}是之前设置的属性。

maven(1):使用 eclipse 构建maven web项目_xml_11

新建Dependency:

Group Id:org.springframework

Artifact Id:spring-webmvc

Version:${springVersion}

点击ok按钮。

说明:该过程是加入springframe的spring-webmvc依赖库,${springVersion}是之前设置的属性。

 

 

 

 

依赖库设定完之后,如果本地不存在还需要从网络上下载相应的依赖库,选中pom.xml文件,右击鼠标选中Run AS – maven install,然后系统自动从网络上下载相应的依赖库。

maven(1):使用 eclipse 构建maven web项目_mvc_12

 

依赖库下载完之后,可以在目录JavaResources – Liraries – Maven Dependencies中看到相应的库文件,如下图所示:

maven(1):使用 eclipse 构建maven web项目_spring_13

 

在src – main目录下新建文件夹​​Java​​。

maven(1):使用 eclipse 构建maven web项目_mvc_14

 

在java中新建类Hello.java。包名为com.springmvc.controller。

maven(1):使用 eclipse 构建maven web项目_xml_15

 

Hello.java中的内容如下:

 



[java]  ​​view plain​​​  ​​​copy​

 


​​​​​​


  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. "message","Hello World!!!");
  10. return "HelloWorld";
  11. }

  12. }



 

 

在src – main –webapp – WEB-INF目录下新建文件夹view,并新建文件HelloWorld.jsp。

maven(1):使用 eclipse 构建maven web项目_mvc_16

Helloworld.jsp文件内容如下所示:



[html]  ​​view plain​​​  ​​​copy​

 


​​​​​​


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. >
  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>



 

 

选中web.xml文件,双击打开该文件,修改该文件使其如下所示:



[html]  ​​view plain​​​  ​​​copy​

 


​​​​​​


  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>



 

 

在src – main –webapp – WEB-INF目录下新建文件spring-mvc-servlet.xml,文件内容如下所示:

 



[html]  ​​view plain​​​  ​​​copy​

 


​​​​​​


  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. >

  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,所有文件已经建立完毕,现在可以运行该项目,看一下效果如何了,选中该项目(点击com.liuht.springmvc,即该项目的最顶层),点击Run As – Run on Server。

maven(1):使用 eclipse 构建maven web项目_spring_17

 

出现一个界面,让你选中要使用的Web服务器,有两个选项,一个是已存在的服务器,另一个是重新定一个新的服务器,我选择已存在服务器,如果你没有,可以重新建立一个web服务器。

maven(1):使用 eclipse 构建maven web项目_spring_18

 

选中要运行的项目,点击Add>按钮,添加到右边的选择框中,如果右边有其他不需要的项目,可以选中,并点击< Remove按钮删除。配置完成之后,点击Finish按钮。

 

maven(1):使用 eclipse 构建maven web项目_xml_19

 

 

 

 

在Console窗口看到如下内容,说明项目启动成功:

maven(1):使用 eclipse 构建maven web项目_spring_20

 

Eclipse自动打开自己的浏览器,并显示如下内容:

maven(1):使用 eclipse 构建maven web项目_spring_21

 

你也可以打开浏览器输入​​http://localhost:8080/com.liuht.springmvc/​

maven(1):使用 eclipse 构建maven web项目_spring_22


 

出现这个界面说明项目已经成功了,Hello World!这串字符来自控制器Hello.java文件。