創(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è)可用