【初】使用maven在Eclipse中搭建SpringMVC

(1)eclipse中配置maven:
(2)命令行創(chuàng)建spring mvc目錄結(jié)構(gòu):
1卖怜、在需要創(chuàng)建的目錄下運行:
mvn archetype:generate -DgroupId=SpringStudy -DartifactId=helloSpringMvc -DarchetypeArtifactId=maven-archetype-webapp
2福澡、將項目導入eclipse:
Import——Maven ——Existing Maven Project
3、java文件夾缺失手動添加:

1.png

(3)編寫配置文件:
1纲缓、pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringStudy </groupId>
<artifactId>helloSpringMvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloSpringMvc Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<commons-lang.version>2.6</commons-lang.version>
<slf4j.version>1.7.6</slf4j.version>
<spring.version>4.1.3.RELEASE</spring.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>

</dependencies>
<build>
<finalName>helloSpringMvc</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.2.v20140723</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、web.xml文件:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>

<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3喊废、mvc-dispatcher-servlet.xml文件:創(chuàng)建路徑如下:

3.png

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config/>


<context:component-scan base-package="com.test">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4祝高、HelloMVCController.java文件:
路徑如下:

4.png

package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hello")
public class HelloMVCController {

@RequestMapping("/mvc")
//host:8080/hello/mvc
public String helloMvc() {
return "home";
}
}
(5)在tomcat中運行:

44png.png

(5)使用jetty運行:
因為在pom.xml文件中加入了jetty插件,可以幫助程序運行污筷,在項目根目錄下工闺,輸入命令:
mvn jetty:run

44png.png

【參】
搭建IntelliJ IDEA+maven+jetty+SpringMVC 開發(fā)環(huán)境
搭建IntelliJ IDEA+maven+jetty+SpringMVC 開發(fā)環(huán)境(二)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子陆蟆,更是在濱河造成了極大的恐慌雷厂,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,029評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件遍搞,死亡現(xiàn)場離奇詭異罗侯,居然都是意外死亡,警方通過查閱死者的電腦和手機溪猿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,395評論 3 385
  • 文/潘曉璐 我一進店門钩杰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人诊县,你說我怎么就攤上這事讲弄。” “怎么了依痊?”我有些...
    開封第一講書人閱讀 157,570評論 0 348
  • 文/不壞的土叔 我叫張陵避除,是天一觀的道長。 經(jīng)常有香客問我胸嘁,道長瓶摆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,535評論 1 284
  • 正文 為了忘掉前任性宏,我火速辦了婚禮群井,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘毫胜。我一直安慰自己书斜,他們只是感情好,可當我...
    茶點故事閱讀 65,650評論 6 386
  • 文/花漫 我一把揭開白布酵使。 她就那樣靜靜地躺著荐吉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪口渔。 梳的紋絲不亂的頭發(fā)上样屠,一...
    開封第一講書人閱讀 49,850評論 1 290
  • 那天,我揣著相機與錄音缺脉,去河邊找鬼瞧哟。 笑死,一個胖子當著我的面吹牛枪向,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咧党,決...
    沈念sama閱讀 39,006評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼秘蛔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起深员,我...
    開封第一講書人閱讀 37,747評論 0 268
  • 序言:老撾萬榮一對情侶失蹤负蠕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后倦畅,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體遮糖,經(jīng)...
    沈念sama閱讀 44,207評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,536評論 2 327
  • 正文 我和宋清朗相戀三年叠赐,在試婚紗的時候發(fā)現(xiàn)自己被綠了欲账。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,683評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡芭概,死狀恐怖赛不,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情罢洲,我是刑警寧澤踢故,帶...
    沈念sama閱讀 34,342評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站惹苗,受9級特大地震影響殿较,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜桩蓉,卻給世界環(huán)境...
    茶點故事閱讀 39,964評論 3 315
  • 文/蒙蒙 一淋纲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧触机,春花似錦帚戳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,772評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蔬胯,卻和暖如春对供,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背氛濒。 一陣腳步聲響...
    開封第一講書人閱讀 32,004評論 1 266
  • 我被黑心中介騙來泰國打工产场, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人舞竿。 一個月前我還...
    沈念sama閱讀 46,401評論 2 360
  • 正文 我出身青樓京景,卻偏偏與公主長得像,于是被迫代替她去往敵國和親骗奖。 傳聞我的和親對象是個殘疾皇子确徙,可洞房花燭夜當晚...
    茶點故事閱讀 43,566評論 2 349

推薦閱讀更多精彩內(nèi)容