将普通Maven Spring项目转换成Web项目的方法
最近在按照李刚的《疯狂J2EE》一书学习Spring,其中第7章的一些代码是ant编译的,而公司用的是Maven,所以想要将其部署并转换成Maven Spring Web项目来执行一下。
本文用的是《疯狂j2ee》第7章的request_scope代码;
1、建立普通的maven project;
2、转换成web项目,这里选2.5版本,注意有的WEB项目的webcontent目录会有所不同,可以点击”further configuration available”按钮进行设定,设定的时候,路径从src目录开始复制即可。
3、把光盘的代码复制到相应的位置
这里看到test.jsp报错了,因为没有Spring的jar包导致,第4步会在Pom.xml中配置这个依赖;
4、在pom.xml中增加依赖的Spring的JAR包
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pss</groupId>
<artifactId>chapter75-requestScope</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
</dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pss</groupId>
<artifactId>chapter75-requestScope</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
</dependencies>
</project>
|
5、在web.xml中配置部署Spring配置文件
<?xml version="1.0" encoding="GBK"?>
<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_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="GBK"?>
<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_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
|
6、设定将Maven的jar包也发布到Tomcat的web目录中,如果依赖了其他的子项目,也需要在这里进行添加,举个例子,如果你的项目用到了项目组的common的JAR包,那么在这里需要也加上才能正确的被部署。
7、新建一个server,把该web项目部署在其中
8、修改server中tomcat的路径并启动server
运行,看到结果;
总结:
1、需要将项目转成web项目;
2、需要在pom中引入spring的context和web两个依赖包;
3、需要在项目部署的设置中,将maven的包也部署一下;
3、需要在web.xml中指定applicationContext.xml的部署位置;
本文地址:http://www.crazyant.net/1607.html