一.使用myeclipse 创建一个新的 maven项目.
(ps:1.在filter过滤的时候输入 webapp 选择"maven-archetype-webapp". 2.在main下建一个java文件夹(建source folder可能不能成功))
具体可参考:http://www.cnblogs.com/waniu/p/3798775.html
二.将project 转变成webproject.(右键--properties--myeclipse--project facets-->勾选"dynamic web module")(如果已经是web 可省去此操作.)
三.为project赋予spring mvc的特性.
在WEB-INF目录下建立:applicationContext.xml,**-servlet.xml并且配置web.xml三个文件.分别为
web.xml
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特性:
HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;
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
http://blog.csdn.net/sapphire_aling/article/details/6947108