Intellij IDEA創建git,maven的SpringMVC項目
原文鏈接:http://www.cnblogs.com/blog5277/p/8906120.html
原文作者:博客園--曲高終和寡
******************************************************
1.在github上(或者私有git)創建git倉庫,注意,如果是先建項目,再建git,在git選擇本地倉庫的時候會提示不是空目錄

2.復制倉庫的git鏈接

3.打開source tree(或者其他的git可視化工具),新建一個文件夾作為本地倉庫

這里新建一個文件夾,克隆

這樣git的部分就建好了
4,打開idea-->file-->new-->new project-->maven-->next

5.這個groupid,就是你們項目組的名字,是什么公司就寫什么,個人的話想寫什么就寫什么,artifactid是這個項目的名字

6.選擇剛剛你新建的git倉庫的路徑,建立項目

7,這時候你打開總目錄就會看到,有兩個紅的,紅色名字說明git里沒有收錄這兩個文件,也說明了git使用成功了

這時候再打開source tree,就能看到里面有一些文件了,這就說明git使用成功了

8.在總目錄下,右鍵-->new-->file(這一步以后再做的時候可以省略,直接把這個文件復制粘貼到新項目里就行了)

起名 .gitignore

復制以下內容,粘貼進去
# 忽略idea下所有文件
.idea/
# 忽略out下生成文件
out/
classes/
# 忽略target下生成文件
target/
# 忽略項目.iml
*.iml
# 忽略word生成的臨時文件
~*
# 忽略lib下生成文件
如下圖所示:

這時候再打開你的source tree,你會發現雜七雜八的文件沒了,只有你需要的文件了
9.在總目錄下,右鍵-->添加框架支持

往下翻,找到Spring MVC

添加完之后就會發現目錄下多了web的文件夾

編輯pom.xml文件,在圖中所示的地方加入以下代碼

<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.5.RELEASE</version> </dependency> </dependencies>
在pom.xml文件上,右鍵-->maven-->reimport

在web文件夾上,右鍵-->新建-->文件夾-->起名 WEB-INF ,然后刪除掉index.jsp

在WEB-INF文件夾上,右鍵-->新建-->xml-->spring配置(這一步和后面的web.xml配置,以后可以反復利用,直接復制粘貼過去就行了)

起名 DispatcherServlet-context.xml,編輯內容,全部刪掉,換成以下內容
<?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"> <mvc:annotation-driven/> <context:component-scan base-package="com.gutongxue"/> <mvc:resources mapping="/resource/**" location="/resource/"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
如下圖所示,現在紅着沒關系

在WEB-INF文件夾上,右鍵-->新建-->文件-->起名 web.xml

粘貼以下內容
<?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"> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/DispatcherServlet-context.xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在項目總目錄,右鍵-->打開module設置

在這里,點+號

打鈎,OK

這里,+號,一路OK

完事后是這樣

在這里,要是沒有Artifacts的話添加一個,如圖這樣的,war exploded

將右邊的全選,右鍵放入,OK

在圖中這兩個地方創建文件夾,你就會看到之前的xml配置文件已經不紅了,注意,在java下,可不是創建了一個叫做 com.gutongxue 的文件夾,而是先創建一個com文件夾,再在com下創建gutongxue文件夾

在圖中位置,創建controller,這個自己敲代碼吧,練習一下

再在WEB-INF的jsp路徑下,創建return出來同名的jsp
然后點這里

點+號

往下翻找到這里

起個名,在這里找到+號

點Artifact

變成這樣,OK

點啟動,這倆方式任選其一

啟動成功

最后一步,將上述東西推送至git,打開source tree,你會發現多了一些我們建的文件

點擊提交,把文件全選,填入說明,點擊提交

點擊推送,全選,推送

稍等片刻,等推送完就可以再這里看到你的記錄了,那么今天的教程到此結束

