使用idea基于maven創(chuàng)建SpringMVC項(xiàng)目

首先我們可以創(chuàng)建maven項(xiàng)目,file-> new ->project->maven?


如果下載不較慢稀并,可以添加屬性name: archetypeCatalog ,value: internal可以加快下載速度


在選擇自己的maven時(shí)蝴蜓。默認(rèn)的下載存儲(chǔ)位置是在C盤碟绑,可以打開(kāi)配置文件更改為自己的下載位置:<localRepository>E:\Program Files\apache-maven-3.6.1\repository_file</localRepository>



項(xiàng)目創(chuàng)建完成后你會(huì)看到,其中java励翼、resources和test文件夾沒(méi)有就自己創(chuàng)建蜈敢,


創(chuàng)建好了之后是灰色的,需要選中相應(yīng)的文件夾右鍵 選擇Mark Directory as 點(diǎn)擊相應(yīng)的顏色即可


打開(kāi)pom.xml文件,添加各類依賴汽抚,將下面的代碼復(fù)制到標(biāo)簽中(這些是我添加的依賴抓狭,做個(gè)參考),添加完成之后造烁,等待maven將這些jar下載完畢否过,下載完成后午笛,可以看到External Library下多了很多文件



```

<properties>

? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

? ? <maven.compiler.source>1.7</maven.compiler.source>

? ? <maven.compiler.target>1.7</maven.compiler.target>

? ? <!--spring 版本號(hào)-->

? ? <spring.version>5.1.6.RELEASE</spring.version>

? ? <!--mybatis 版本號(hào)-->

? ? <mybatis.version>3.2.4</mybatis.version>

</properties>

<!--上邊介紹的的版本號(hào)等等 將下面的代碼放到dependencies標(biāo)簽中即可-->

? ? <!--spring 核心包-->

? ? <!-- spring start -->

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-core</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-web</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-oxm</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-tx</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-jdbc</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-webmvc</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-aop</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-context-support</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-test</artifactId>

? ? ? <version>${spring.version}</version>

? ? </dependency>

? ? <!-- spring end -->

? ? <!--mybatis核心包-->

? ? <dependency>

? ? ? <groupId>org.mybatis</groupId>

? ? ? <artifactId>mybatis</artifactId>

? ? ? <version>${mybatis.version}</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>junit</groupId>

? ? ? <artifactId>junit</artifactId>

? ? ? <version>4.11</version>

? ? ? <scope>test</scope>

? ? </dependency>

? ? <!--日志-->

? ? <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->

? ? <dependency>

? ? ? <groupId>org.slf4j</groupId>

? ? ? <artifactId>slf4j-log4j12</artifactId>

? ? ? <version>1.8.0-alpha0</version>

? ? ? <scope>test</scope>

? ? </dependency>

? ? <!--j2ee相關(guān)包 servlet、jsp苗桂、jstl-->

? ? <dependency>

? ? ? <groupId>javax.servlet</groupId>

? ? ? <artifactId>javax.servlet-api</artifactId>

? ? ? <version>4.0.0</version>

? ? ? <scope>provided</scope>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>javax.servlet.jsp</groupId>

? ? ? <artifactId>jsp-api</artifactId>

? ? ? <version>2.2</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>javax.servlet</groupId>

? ? ? <artifactId>jstl</artifactId>

? ? ? <version>1.2</version>

? ? </dependency>

? ? <!--mybatis/spring 包-->

? ? <dependency>

? ? ? <groupId>org.mybatis</groupId>

? ? ? <artifactId>mybatis-spring</artifactId>

? ? ? <version>1.2.2</version>

? ? </dependency>

? ? <!--MySQL 驅(qū)動(dòng)包-->

? ? <dependency>

? ? ? <groupId>mysql</groupId>

? ? ? <artifactId>mysql-connector-java</artifactId>

? ? ? <version>5.1.39</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>org.apache.maven</groupId>

? ? ? <artifactId>maven-model</artifactId>

? ? ? <version>3.0</version>

? ? </dependency>

? ? <dependency>

? ? ? <groupId>javax.servlet</groupId>

? ? ? <artifactId>javax.servlet-api</artifactId>

? ? ? <version>3.1.0</version>

? ? </dependency>

```

之后添加springMVC框架药磺,右擊項(xiàng)目文件夾Demo,選擇Add framework support


將下圖中的Spring和Spring下的Spring MVC都勾上煤伟,之前配置pom.xml文件時(shí)癌佩,已經(jīng)自動(dòng)下載了spring相關(guān)文件,所以這里就直接用之前下載好的就可以了便锨,OK围辙。(注意:點(diǎn)了Add framework support之后,在下圖中有可能會(huì)找不到Spring放案,解決辦法在下圖的下方)

如果在Add framework support中找不到Spring姚建,那是因?yàn)轫?xiàng)目中可能已經(jīng)存在Spring相關(guān)文件,但不一定是完善的吱殉。因此我們要將已經(jīng)存在的Spring給刪掉掸冤,重新添加,方法如下:

點(diǎn)擊File友雳,選擇Project Structure稿湿,(快捷鍵ctrl+shift+alt+s)選擇Facets,就會(huì)看到有一個(gè)Spring啦沥阱,右擊它缎罢,點(diǎn)刪除就行啦,然后再回到上面第3步重新Add framework support考杉,Spring就會(huì)出現(xiàn)啦


Spring框架添加完之后策精,會(huì)看到目錄下多了兩個(gè)xml文件,我還創(chuàng)建了static文件夾用來(lái)存放靜態(tài)資源css崇棠、js咽袜、images(圖片等),和views文件夾用來(lái)存放映射文件(例如JSP


其中java文件是用來(lái)建包的枕稀,現(xiàn)在可以對(duì)SpringMVC進(jìn)行設(shè)置了询刹,首先配置web.xml 。他自己自動(dòng)生成的可能不是這種萎坷,直接用下面的替換就可以了

```

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

? ? ? ? xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

? ? ? ? version="3.1">

? ? <display-name>Archetype Created Web Application</display-name>

? ? <!--welcome pages-->

? ? <welcome-file-list>

? ? ? ? <welcome-file>index.jsp</welcome-file>

? ? </welcome-file-list>

? ? <!--配置springmvc DispatcherServlet-->

? ? <servlet>

? ? ? ? <servlet-name>springMVC</servlet-name>

? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

? ? ? ? <init-param>

? ? ? ? ? ? <!--配置dispatcher.xml作為mvc的配置文件-->

? ? ? ? ? ? <param-name>contextConfigLocation</param-name>

? ? ? ? ? ? <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

? ? ? ? </init-param>

? ? ? ? <load-on-startup>1</load-on-startup>

? ? ? ? <async-supported>true</async-supported>

? ? </servlet>

? ? <!--<servlet>-->

? ? ? ? <!--<servlet-name>EmpServlet</servlet-name>-->

? ? ? ? <!--<servlet-class>com.gx.filter.EmpServlet</servlet-class>-->

? ? <!--</servlet>-->

? ? <servlet-mapping>

? ? ? ? <servlet-name>springMVC</servlet-name>

? ? ? ? <url-pattern>/</url-pattern>

? ? </servlet-mapping>

? ? <!--把a(bǔ)pplicationContext.xml加入到配置文件中-->

? ? <context-param>

? ? ? ? <param-name>contextConfigLocation</param-name>

? ? ? ? <param-value>/WEB-INF/applicationContext.xml</param-value>

? ? </context-param>

? ? <listener>

? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

? ? </listener>

</web-app>

```

配置dispatcher-servlet.xml凹联,負(fù)責(zé)mvc的配置

```

<?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:mvc="http://www.springframework.org/schema/mvc"

? ? ? xmlns:context="http://www.springframework.org/schema/context"

? ? ? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

? <!--此文件負(fù)責(zé)整個(gè)mvc中的配置-->

? <!--啟用spring的一些annotation -->

? <context:annotation-config/>

? <!-- 配置注解驅(qū)動(dòng) 可以將request參數(shù)與綁定到controller參數(shù)上 -->

? <mvc:annotation-driven/>

? <!--靜態(tài)資源映射-->

? <!--本項(xiàng)目把靜態(tài)資源放在了webapp的statics目錄下,資源映射如下-->

? <mvc:resources mapping="/css/**" location="/static/css/"/>

? <mvc:resources mapping="/js/**" location="/static/js/"/>

? <mvc:resources mapping="/image/**" location="/static/images/"/>

? <mvc:default-servlet-handler />? <!--這句要加上哆档,要不然可能會(huì)訪問(wèn)不到靜態(tài)資源蔽挠,具體作用自行百度-->

? <!-- 對(duì)模型視圖名稱的解析,即在模型視圖名稱添加前后綴(如果最后一個(gè)還是表示文件夾,則最后的斜杠不要漏了) 使用JSP-->

? <!-- 默認(rèn)的視圖解析器 在上邊的解析錯(cuò)誤時(shí)使用 (默認(rèn)使用html)- -->

? <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

? ? ? <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

? ? ? <property name="prefix" value="/WEB-INF/views/"/><!--設(shè)置JSP文件的目錄位置-->

? ? ? <property name="suffix" value=".jsp"/>

? ? ? <property name="exposeContextBeansAsAttributes" value="true"/>

? </bean>

? <!-- 自動(dòng)掃描裝配 -->

? <context:component-scan base-package="com.gx.controller"/>

</beans>

```

配置applicationContext.xml瓜浸,負(fù)責(zé)一些非MVC組件的配置澳淑,暫時(shí)沒(méi)有所以是空的比原,但也可以掃描一下

```

<?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:context="http://www.springframework.org/schema/context"

? ? ? 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">

? ? <context:component-scan base-package="com.gx"/>

</beans>

```

以上就是配置好了 可以創(chuàng)建文件使用了哦!親測(cè)可用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末杠巡,一起剝皮案震驚了整個(gè)濱河市量窘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌氢拥,老刑警劉巖蚌铜,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異兄一,居然都是意外死亡厘线,警方通過(guò)查閱死者的電腦和手機(jī)识腿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門出革,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人渡讼,你說(shuō)我怎么就攤上這事骂束。” “怎么了成箫?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵展箱,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我蹬昌,道長(zhǎng)混驰,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任皂贩,我火速辦了婚禮栖榨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘明刷。我一直安慰自己婴栽,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布辈末。 她就那樣靜靜地躺著愚争,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挤聘。 梳的紋絲不亂的頭發(fā)上轰枝,一...
    開(kāi)封第一講書(shū)人閱讀 52,246評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音组去,去河邊找鬼鞍陨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛添怔,可吹牛的內(nèi)容都是我干的湾戳。 我是一名探鬼主播贤旷,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼砾脑!你這毒婦竟也來(lái)了幼驶?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤韧衣,失蹤者是張志新(化名)和其女友劉穎盅藻,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體畅铭,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡氏淑,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了硕噩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片假残。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖炉擅,靈堂內(nèi)的尸體忽然破棺而出辉懒,到底是詐尸還是另有隱情,我是刑警寧澤谍失,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布眶俩,位于F島的核電站,受9級(jí)特大地震影響快鱼,放射性物質(zhì)發(fā)生泄漏颠印。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一抹竹、第九天 我趴在偏房一處隱蔽的房頂上張望线罕。 院中可真熱鬧,春花似錦柒莉、人聲如沸闻坚。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)窿凤。三九已至,卻和暖如春跨蟹,著一層夾襖步出監(jiān)牢的瞬間雳殊,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工窗轩, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留夯秃,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像仓洼,于是被迫代替她去往敵國(guó)和親介陶。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

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