0.准備工作
首先將eclipse和需要的插件准備好,例如maven插件,spring IDE插件。
1.建立maven下的webapp項目
1.新建一個maven項目,類型為webapp,如下圖

2.然后給項目命名,加入groupId等

3.配置項目的發布目錄,在 Deployment Assemly下,如圖

2.配置Spring和Maven
1.配置pom.xml,添加如下包依賴。版本不一定要對應,后邊可能會用到些新的包,缺少哪些包可以后續去百度然后加入到pom.xml中
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency>
2.配置web.xml文件,添加ContextLoaderListener監聽器,在servlet啟動時會去裝配制定配置文件的配置;然后添加springDispatcherServlet,指定mvc配置文件,配置mvc框架。全部配置如下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-context.xml, /WEB-INF/spring-hibernate.xml </param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 設置監聽位置,'/'為全部監聽 --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.配置spring-mvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 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-4.1.xsd"> <!-- 激活@controller模式 --> <mvc:annotation-driven /> <!-- 配置包掃描位置(會在此包下掃描@controller控制器) --> <context:component-scan base-package="com.test.maven.controller" /> <!-- 配置視圖解析器(jsp文件前綴后綴) --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- 配置tiles模板(沒有用到tiles可以不用配置此項) --> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles/tiles-definitions.xml</value> </list> </property> </bean> </beans>
4.配置spring-context.xml。暫時只建立最簡單的框架,所以這里暫時可以不用配置。
到此最基本的springMVC框架就搭建好了。把項目裝入tomcat,運行訪問項目的主目錄,就會調用默認的index.jsp,顯示hello world了。
