一.使用myeclipse 建立一個新的 maven項目.html
(ps:1.在filter過濾的時候輸入 webapp 選擇"maven-archetype-webapp". 2.在main下建一個java文件夾(建source folder可能不能成功))前端
具體可參考:http://www.cnblogs.com/waniu/p/3798775.htmljava
二.將project 轉變成webproject.(右鍵--properties--myeclipse--project facets-->勾選"dynamic web module")(若是已是web 可省去此操做.)web
三.爲project賦予spring mvc的特性.spring
在WEB-INF目錄下創建:applicationContext.xml,**-servlet.xml而且配置web.xml三個文件.分別爲apache
web.xmlspring-mvc
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!--listener裏邊配置的是,表示項目啓動時候會去讀取applicationContext.xml,去實例化bean--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>exam</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>exam</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
啓動時會默認在/WEB-INF目錄下查找applicationContext.xml做爲spring容器的配置文件,這裏能夠初始化一些bean,如DataSource。咱們這裏什麼也不作。代碼以下:applicationContext.xml(暫時不配置具體bean)服務器
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> </beans>
啓動時也會默認在/WEB-INF目錄下查找XXX-servlet.xml做爲配置文件,XXX就是DispatcherServlet的名字,該文件中將配置兩項重要的mvc特性:mvc
HandlerMapping,負責爲DispatcherServlet這個前端控制器的請求查找Controller;app
ViewResolver,負責爲DispatcherServlet查找ModelAndView的視圖解析器。
<?xml version="1.0" encoding="UTF-8"?> <!-- Bean頭部 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 激活@Controller模式 --> <mvc:annotation-driven /> <!-- 對包中的全部類進行掃描,以完成Bean建立和自動依賴注入的功能 須要更改 --> <context:component-scan base-package="cc.monggo.web.controller" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <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> </beans>
四: 配置pom.xml,讓maven解決jar包依賴問題
pom.xml:
pop.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>exam</groupId> <artifactId>exam_3</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>exam_3 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <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>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_2.5_spec</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>exam_3</finalName> </build> </project>
這樣一個最基本spring mvc項目就配置好了
此時對於實現部署測試過的項目啓動時候可能會引起:
嚴重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
從新部署服務器,試試應該就能夠了.
五: 打包部署:
maven項目打包很容易,直接右鍵項目 run as --maven install 就會在 target目錄下構建 ***.war 部署包,將部署包放入到應用服務器下,便可完成部署運行.
參考文章:
http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html