使用maven一步一步構建spring mvc項目


1      使用eclipse構建maven web項目

1.1新建Maven的web項目

打開菜單File –New-MavenProject。

maven_web_spring_mvc_new

點擊Next

 

 

選擇模板類型archtype——maven-archtype-webapp。然后點擊Next。

 

輸入Group Id和artifact Id。Group Id一般填入項目名稱,Artifact Id一般填入子項目的名稱。

 

生成的項目文件結構如下所示:

 

選擇pom.xml文件,並打開,界面如下所示:

 

增加Properties:展開Properties選項,然后點擊Create…按鈕,如下所示:然后Name字段填入springVersion,Value字段填入3.2.5.RELEASE。即在pom.xml中增加了一個屬性springVersion,屬性值為3.2.5.RELEASE。

 

選擇Dependencies標簽,打開Dependencies選項卡,並增加一個新的Dependency。

 

 

Group Id:org.springframework

Artifact Id:spring-web

Version:${springVersion}

點擊ok按鈕。

說明:該過程是加入springframe的spring-web依賴庫,${springVersion}是之前設置的屬性。

新建Dependency:

Group Id:org.springframework

Artifact Id:spring-webmvc

Version:${springVersion}

點擊ok按鈕。

說明:該過程是加入springframe的spring-webmvc依賴庫,${springVersion}是之前設置的屬性。

 

 

 

 

依賴庫設定完之后,如果本地不存在還需要從網絡上下載相應的依賴庫,選中pom.xml文件,右擊鼠標選中Run AS – maven install,然后系統自動從網絡上下載相應的依賴庫。

 

依賴庫下載完之后,可以在目錄JavaResources – Liraries – Maven Dependencies中看到相應的庫文件,如下圖所示:

 

在src – main目錄下新建文件夾Java

 

在java中新建類Hello.java。包名為com.springmvc.controller。

 

Hello.java中的內容如下:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. package com.springmvc.controller;  
  2.   
  3.   
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.ui.Model;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7.   
  8. @Controller  
  9. public class Hello {  
  10.     @RequestMapping(value="/Hello")  
  11.     public String HelloWorld(Model model){  
  12.         model.addAttribute("message","Hello World!!!");  
  13.         return "HelloWorld";  
  14.     }  
  15.       
  16. }  

 

 

在src – main –webapp – WEB-INF目錄下新建文件夾view,並新建文件HelloWorld.jsp。

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

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  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>  

 

 

選中web.xml文件,雙擊打開該文件,修改該文件使其如下所示:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  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.   
  10.     <servlet-mapping>  
  11.         <servlet-name>spring-mvc</servlet-name>  
  12.         <url-pattern>/</url-pattern>  
  13.     </servlet-mapping>  
  14. </web-app>  

 

 

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

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  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.   
  12.     <context:component-scan base-package="com.springmvc.controller" />  
  13.     <bean id="viewResolver"  
  14.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  15.         <property name="prefix" value="/WEB-INF/view/" />  
  16.         <property name="suffix" value=".jsp" />  
  17.     </bean>  
  18. </beans>  



 

Ok,所有文件已經建立完畢,現在可以運行該項目,看一下效果如何了,選中該項目(點擊com.liuht.springmvc,即該項目的最頂層),點擊Run As – Run on Server。

 

出現一個界面,讓你選中要使用的Web服務器,有兩個選項,一個是已存在的服務器,另一個是重新定一個新的服務器,我選擇已存在服務器,如果你沒有,可以重新建立一個web服務器。

 

選中要運行的項目,點擊Add>按鈕,添加到右邊的選擇框中,如果右邊有其他不需要的項目,可以選中,並點擊< Remove按鈕刪除。配置完成之后,點擊Finish按鈕。

 

 

 

 

 

在Console窗口看到如下內容,說明項目啟動成功:

 

Eclipse自動打開自己的瀏覽器,並顯示如下內容:

 

你也可以打開瀏覽器輸入http://localhost:8080/com.liuht.springmvc/


 

出現這個界面說明項目已經成功了,Hello World!這串字符來自控制器Hello.java文件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM