- 創(chuàng)建工程和Maven model锥忿,并支持Spring framework
- 引入jar包,commons-logging寸认、AOP签财、beans、Content偏塞、Core唱蒸、Expression、WebWebMVC烛愧,順便將數(shù)據(jù)庫(kù)處理和其他jar包引入
- 配置Web.XML油宜,將DispatcherServlet用于處理所以Web請(qǐng)求掂碱,可順便添加過(guò)濾器
- 添加SpringConfig.xml,配置掃描目錄和視圖解析器等慎冤。注意頭部的描述疼燥,
- 編寫Controller,以及視圖文件
創(chuàng)建工程
選擇空工程
選擇空工程
輸入工程名稱蚁堤,和工程存儲(chǔ)位置
輸入工程名稱醉者,選擇位置
下一步
創(chuàng)建Maven模型,Web-APP
創(chuàng)建新Model
選擇Model
寫好Maven的倆參數(shù)披诗,GroupId和ArtefactId撬即,版本嘛,菜鳥(niǎo)忽略
選好你的Maven setting.xml文件呈队,配置好你的Maven倉(cāng)庫(kù)剥槐!
寫上你的Model的名稱
選擇finish就OK了
記得pom.xml文件要設(shè)為可編輯狀態(tài)
在Model文件夾上,右鍵點(diǎn)擊
Paste_Image.png
選擇Spring框架支持
菜鳥(niǎo)請(qǐng)勾上Create empty spring-config.xml
如果你有本地Maven庫(kù)的話宪摧,選擇Set up library later
沒(méi)有可以選擇Download
項(xiàng)目結(jié)構(gòu)變成了下面的樣子
Paste_Image.png
修改工程結(jié)構(gòu)(個(gè)人習(xí)慣)
這樣滴
修改pom.xml
<dependencies>
下加上節(jié)點(diǎn)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
這樣SpringMVC的jar包就引入完成了
- 配置DispatcherServlet
在WEB.xml文件中粒竖,創(chuàng)建一個(gè)Servlet節(jié)點(diǎn),將Spring的核心Servlet配置進(jìn)去,加入Spring配置文件參數(shù)(就是Spring配置的XML文件几于,不建議不寫這個(gè)參數(shù))蕊苗,和自啟動(dòng)參數(shù)
注意一下WEB.XML的描述,最好參考一下Tomcat最新的WEB.XML頭沿彭,因?yàn)镴avaEE一直都在更新朽砰,所以版本號(hào)會(huì)漸漸過(guò)時(shí)
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置掃包目錄:
最好新建一個(gè)SpringConfig的XML配置文件,當(dāng)然你也可以使用自動(dòng)生成的Spring配置文件喉刘,當(dāng)然瞧柔,描述頭一定要注意,否則就找不到正確的節(jié)點(diǎn)解析方法,下面雖然有些描述文件暫不需要饱搏,但是還是加上了
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.zing"></context:component-scan>
</beans>
現(xiàn)在可以寫Controller 和 頁(yè)面了運(yùn)行也是沒(méi)問(wèn)題非剃,不過(guò)沒(méi)有精致的頁(yè)面,只有自帶的Hello World推沸。