1使用eclipse構建maven web項目
1.1新建maven的web項目
打開菜單file –new-mavenproject。
點擊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中的內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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" ; } } |
在src – main –webapp – web-inf目錄下新建文件夾view,并新建文件helloworld.jsp。
helloworld.jsp文件內容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ 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> |
選中web.xml文件,雙擊打開該文件,修改該文件使其如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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> |
在src – main –webapp – web-inf目錄下新建文件spring-mvc-servlet.xml,文件內容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<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 = "orgspringframeworkwebservletviewinternalresourceviewresolver" > <property name= "prefix" value= "/web-inf/view/" /> <property name= "suffix" value= ".jsp" /> </bean> </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文件。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/zhuawang/p/5651896.html