使用maven一步一步構(gòu)建spring mvc項(xiàng)目(圖文詳解)
1使用eclipse構(gòu)建maven web項(xiàng)目
1.1新建Maven的web項(xiàng)目
打開菜單File –New-MavenProject。

點(diǎn)擊Next

選擇模板類型archtype——maven-archtype-webapp。然后點(diǎn)擊Next。

輸入Group Id和artifact Id。Group Id一般填入項(xiàng)目名稱,Artifact Id一般填入子項(xiàng)目的名稱。

生成的項(xiàng)目文件結(jié)構(gòu)如下所示:

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

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

選擇Dependencies標(biāo)簽,打開Dependencies選項(xiàng)卡,并增加一個(gè)新的Dependency。



Group Id:org.springframework
Artifact Id:spring-web
Version:${springVersion}
點(diǎn)擊ok按鈕。
說(shuō)明:該過(guò)程是加入springframe的spring-web依賴庫(kù),${springVersion}是之前設(shè)置的屬性。

新建Dependency:
Group Id:org.springframework
Artifact Id:spring-webmvc
Version:${springVersion}
點(diǎn)擊ok按鈕。
說(shuō)明:該過(guò)程是加入springframe的spring-webmvc依賴庫(kù),${springVersion}是之前設(shè)置的屬性。
依賴庫(kù)設(shè)定完之后,如果本地不存在還需要從網(wǎng)絡(luò)上下載相應(yīng)的依賴庫(kù),選中pom.xml文件,右擊鼠標(biāo)選中Run AS – maven install,然后系統(tǒng)自動(dòng)從網(wǎng)絡(luò)上下載相應(yīng)的依賴庫(kù)。

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

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

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

Hello.java中的內(nèi)容如下:
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文件內(nèi)容如下所示:
<%@ 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文件,雙擊打開該文件,修改該文件使其如下所示:
<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,文件內(nèi)容如下所示:
<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,所有文件已經(jīng)建立完畢,現(xiàn)在可以運(yùn)行該項(xiàng)目,看一下效果如何了,選中該項(xiàng)目(點(diǎn)擊com.liuht.springmvc,即該項(xiàng)目的最頂層),點(diǎn)擊Run As – Run on Server。

出現(xiàn)一個(gè)界面,讓你選中要使用的Web服務(wù)器,有兩個(gè)選項(xiàng),一個(gè)是已存在的服務(wù)器,另一個(gè)是重新定一個(gè)新的服務(wù)器,我選擇已存在服務(wù)器,如果你沒(méi)有,可以重新建立一個(gè)web服務(wù)器。

選中要運(yùn)行的項(xiàng)目,點(diǎn)擊Add>按鈕,添加到右邊的選擇框中,如果右邊有其他不需要的項(xiàng)目,可以選中,并點(diǎn)擊< Remove按鈕刪除。配置完成之后,點(diǎn)擊Finish按鈕。

在Console窗口看到如下內(nèi)容,說(shuō)明項(xiàng)目啟動(dòng)成功:

Eclipse自動(dòng)打開自己的瀏覽器,并顯示如下內(nèi)容:

你也可以打開瀏覽器輸入http://localhost:8080/com.liuht.springmvc/
出現(xiàn)這個(gè)界面說(shuō)明項(xiàng)目已經(jīng)成功了,Hello World!這串字符來(lái)自控制器Hello.java文件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java map.getOrDefault()方法的用法詳解
這篇文章主要介紹了Java map.getOrDefault()方法的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格示例代碼
最近工作中遇到一個(gè)需求,是需要導(dǎo)出數(shù)據(jù)到Excel表格里,所以寫個(gè)Demo測(cè)試一下,還是比較簡(jiǎn)單的,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-10-10
解決idea出現(xiàn)的java.lang.OutOfMemoryError:?Java?heap?space的問(wèn)題
我們?cè)谑褂胕dea的時(shí)候經(jīng)常會(huì)遇到一些問(wèn)題,本文介紹了如何解決idea出現(xiàn)的java.lang.OutOfMemoryError:?Java?heap?space的問(wèn)題,文中有相關(guān)的圖文示例,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java獲取http和https協(xié)議返回的json數(shù)據(jù)
本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個(gè)方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。2017-01-01
java Swing JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法示例
這篇文章主要介紹了java Swing JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法,結(jié)合實(shí)例形式分析了Swing組件的JFrame框架類中setDefaultCloseOperation方法的簡(jiǎn)單使用技巧,需要的朋友可以參考下2017-11-11
java Timer 定時(shí)每天凌晨1點(diǎn)執(zhí)行任務(wù)
這篇文章主要介紹了java Timer 定時(shí)每天凌晨1點(diǎn)執(zhí)行任務(wù)的代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09


