美文网首页
将普通Maven Spring项目转换成Web项目的方法

将普通Maven Spring项目转换成Web项目的方法

作者: loserStar | 来源:发表于2018-01-11 09:25 被阅读22次

最近在按照李刚的《疯狂J2EE》一书学习Spring,其中第7章的一些代码是ant编译的,而公司用的是Maven,所以想要将其部署并转换成Maven Spring Web项目来执行一下。

本文用的是《疯狂j2ee》第7章的request_scope代码;

1、建立普通的maven project;

1

2

2、转换成web项目,这里选2.5版本的

3

3、把光盘的代码复制到相应的位置

这里看到test.jsp报错了,因为没有Spring的jar包导致,第4步会在Pom.xml中配置这个依赖;

4

4、在pom.xml中增加依赖的Spring的JAR包

<textarea class="crayon-plain print-no" readonly="readonly" cols="22" rows="3" style="box-sizing: border-box; margin: 0px; font-family: "Courier New", monospace !important; font-size: inherit; overflow: auto; vertical-align: top; line-height: 15px !important; outline: 0px; border: 0px; resize: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; opacity: 0; color: rgb(0, 0, 0); z-index: 0;"></textarea>

|

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配置文件

<textarea class="crayon-plain print-no" readonly="readonly" cols="22" rows="3" style="box-sizing: border-box; margin: 0px; font-family: "Courier New", monospace !important; font-size: inherit; overflow: auto; vertical-align: top; line-height: 15px !important; outline: 0px; border: 0px; resize: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; opacity: 0; color: rgb(0, 0, 0); z-index: 0;"></textarea>

|

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目录中

5

7、新建一个server,把该web项目部署在其中

6

8、修改server中tomcat的路径并启动server

8

运行,看到结果;

7

总结:

1、需要将项目转成web项目;

2、需要在pom中引入spring的context和web两个依赖包;

3、需要在项目部署的设置中,将maven的包也部署一下;

3、需要在web.xml中指定applicationContext.xml的部署位置;

本文地址:http://www.crazyant.net/1607.html

相关文章

网友评论

      本文标题:将普通Maven Spring项目转换成Web项目的方法

      本文链接:https://www.haomeiwen.com/subject/ffcznxtx.html