詳解IDEA用maven創(chuàng)建springMVC項(xiàng)目和配置
本文介紹了IDEA用maven創(chuàng)建springMVC項(xiàng)目和配置,分享給大家,具體如下:
工具準(zhǔn)備:IDEA2016.3 Java jdk 1.8
1、DEA創(chuàng)建項(xiàng)目
新建一個(gè)maven project,并且選擇webapp原型。
然后點(diǎn)擊next
這里的GroupId和ArtifactID隨意填寫,但是ArtifactID最好和你的項(xiàng)目一名一樣然后next
為了快一點(diǎn)創(chuàng)建,我們添加一個(gè)屬性值,如圖中亮的所示,點(diǎn)右邊的加號(hào),name=archetypeCatalog value=internal。
這里我們選擇使用IDEA自帶的maven,下面的兩個(gè)分別是你的maven配置文件和你的倉(cāng)庫(kù)地址,我記得idea的maven一開始是沒(méi)有setting.xml的,所以你要去maven的目錄里面拷一份setting.xml到你的倉(cāng)庫(kù)中。idea的maven在安裝路徑的plugins文件夾下,即X:\xxx\JetBrains\IntelliJ IDEA 2016.3.2\plugins\plugins\maven\lib\maven3\conf\setting.xml拷貝到你的用戶文件夾下的.m2文件夾下,為了之后能夠快速的下載依賴包,我們要加一個(gè)官方庫(kù)的鏡像,因?yàn)閙aven的官方倉(cāng)庫(kù)在國(guó)外,太慢了。
我們?cè)谀愕?m2文件夾下的setting.xml中添加如下代碼:
<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror>
具體位置如下圖所示
這個(gè)是阿里的庫(kù),所以很快。做完之后我們回到idea,點(diǎn)擊next,然后填寫項(xiàng)目名,然后finish。
接下來(lái)idea開始創(chuàng)建項(xiàng)目,這里你要把maven自動(dòng)導(dǎo)入打開。
然后等待maven創(chuàng)建項(xiàng)目成功
這樣我們的項(xiàng)目初始的框架就弄好了。
2、添加pom依賴
創(chuàng)建好之后的項(xiàng)目目錄如圖所示
我們打開其中的pom.xml,添加我們的依賴。這里我把我的依賴全部放出來(lái),復(fù)制到你的pom.xml的dependencies標(biāo)簽之間就可以了,pom文件中也會(huì)給你一個(gè)示例。
我的依賴如下
<!--測(cè)試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!--J2EE--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </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> <!--mysql驅(qū)動(dòng)包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!--springframework--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>com.github.stefanbirkner</groupId> <artifactId>system-rules</artifactId> <version>1.16.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!--其他需要的包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
一旦你復(fù)制過(guò)去之后,maven就會(huì)開始下載相應(yīng)的jar文件,等待下載完成即可。可能包有點(diǎn)多,不過(guò)用了阿里的鏡像之后還是挺快的。
3、添加框架支持
配置完pom.xml之后,我們?cè)趇dea中要添加一下框架的支持。
右擊我們的項(xiàng)目文件夾,選擇add framework support
然后在窗口中分別選中spring和springmvc,并且選擇spring是,記得勾選springconfig.xml
因?yàn)槲覀冎跋螺d過(guò)相應(yīng)的文件,所以這里直接用我們下載好的spring文件。
點(diǎn)擊ok之后,我們會(huì)發(fā)現(xiàn)WEB-INF文件夾下多出了兩個(gè)文件
這個(gè)就是我們之后要配置的文件,先不用管。
4、完善目錄結(jié)構(gòu)
首先我們?cè)趕rc/main文件夾下創(chuàng)建java文件夾,你會(huì)發(fā)現(xiàn)這個(gè)文件夾不能創(chuàng)建java的類文件和package,別急,先把必須的文件夾全部創(chuàng)建好。請(qǐng)按照下圖創(chuàng)建文件夾
然后我們進(jìn)入project structure選擇文件夾的作用,在界面的右上角進(jìn)入project structure
然后在module中選擇設(shè)置各個(gè)模塊,其中java文件夾是 sources,test是Test,改完之后,點(diǎn)ok,文件夾會(huì)變色,那就成功了。
然后仔仔java文件夾中添加需要的包。最后的完整目錄如下
這樣我們配置前的工作就完成了,接下來(lái)就是對(duì)springmvc進(jìn)行配置。我把兩種配置的方法分成兩部分,以供參考。
5、基于XML 的配置
5.1、配置web.xml
idea創(chuàng)建的web.xml這個(gè)文件版本比較低,所以我們要找一個(gè)新的。把我的文件直接全部覆蓋復(fù)制進(jìn)去就可以了。
<?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-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>
上面代碼的意思主要就是創(chuàng)建一個(gè)中央的控制器,都有簡(jiǎn)單的注釋,如果有需要,請(qǐng)自行百度。
5.2 配置dispatcher-servlet.xml
這個(gè)文件負(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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!--此文件負(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="/WEB-INF/statics/css/"/> <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/> <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/> <!-- 對(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="example.controller"/> </beans>
直接復(fù)制即可,都有注釋,如果有需要,請(qǐng)自行百度。
5.3、 配置applicationContext.xml
其實(shí)這個(gè)文件沒(méi)什么好配置的,這個(gè)文件主要負(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="example"/> </beans>
5.4、 測(cè)試
三個(gè)配置文件配置好之后,就可以測(cè)試了。首先在controller文件夾下創(chuàng)建一個(gè)IndexController,代碼如下:
@Controller @RequestMapping("/home") public class IndexController { @RequestMapping("/index") public String index() { return "index"; } }
views文件夾下創(chuàng)建index.jsp,statics/css/下創(chuàng)建test.css
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index</title> <link rel="stylesheet" type="text/css" href="<c:url value=" rel="external nofollow" /css/test.css"/> "/> </head> <body> <p>Spring MVC based on XML config success!</p> </body> </html>
p { background-color: brown; font-family: "Courier New"; font-size:100px; }
接下來(lái)配置tomcat,如果沒(méi)有,去官網(wǎng)下載tomcat7以上的版本。
右上角
然后選擇tomcat
配置相關(guān)信息
還有deployment
選擇第二個(gè)
這里的名稱和項(xiàng)目名一樣。
然后點(diǎn)擊ok完成。
最后運(yùn)行tomcat,在瀏覽器輸入http://localhost:8080/Demo/home/index 即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用MyEclipse 開發(fā)struts2框架實(shí)現(xiàn)登錄功能(結(jié)構(gòu)教程)
這篇文章主要介紹了使用MyEclipse 開發(fā)struts2框架實(shí)現(xiàn)登錄功能(結(jié)構(gòu)教程)的相關(guān)資料,需要的朋友可以參考下2016-03-03微信企業(yè)號(hào)驗(yàn)證/發(fā)送/接收消息
這篇文章主要介紹了微信企業(yè)號(hào)驗(yàn)證/發(fā)送/接收消息的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10