使用Maven搭建SpringMVC项目的步骤(图文教程)

网友投稿 271 2023-03-31


使用Maven搭建SpringMVC项目的步骤(图文教程)

约定电脑都安装了eclipse,且已配置好Maven以及eclipse插件。

1.Eclipse

2.maven

3.Eclipse 需要安装maven插件。url:maven - http://download.eclipse.org/technology/m2e/releases 。

1、新建一个Maven Project

2、选择工作空间

3、搭建Web工程,我们选择maven-archetype-webapp类型

4、填写项目参数,如图

5、以上步骤完成时的工程结构目录

6、可以查看或修改发布目录

7、确保勾选上Dynamic Web Module和java

8、完成以上步骤,我们的工程就是一个Web项目了,接着我们赋予工程的springmvc特性,配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet。代码如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

classpath*:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

contacts

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:/spring-mvc.xml

1

contacts

*.htm

index.jsp

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

classpath*:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

contacts

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:/spring-mvc.xml

1

contacts

*.htm

index.jsp

9、配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找 applicationContext.xml作为spring容器的配置文件,这里可以初始化一些bean,如DataSource等。代码如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:p="http://springframework.org/schema/p" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd">

destroy-method="close" p:driverClassName="${jdbc.driverClassName}"

p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:p="http://springframework.org/schema/p" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd">

destroy-method="close" p:driverClassName="${jdbc.driverClassName}"

p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

destroy-method="close" p:driverClassName="${jdbc.driverClassName}"

p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

10、配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX- servlet.xml作为配置文件,XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。这里我们使用指定的xml配置文件spring-mvc.xml,代码如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd"

default-lazy-init="true">

text/plain;charset=UTF-8

text/html;charset=UTF-8

application/json;charset=UTF-8

text/plain;charset=UTF-8

text/html;charset=UTF-8

application/json;charset=UTF-8

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd"

default-lazy-init="true">

text/plain;charset=UTF-8

text/html;charset=UTF-8

application/json;charset=UTF-8

text/plain;charset=UTF-8

text/html;charset=UTF-8

application/json;charset=UTF-8

11、配置pom.xml文件,让maven自动配置jar包,代码如下

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

cn.jxufe

SpringMVC-Maven

war

1.0

SpringMVC-Maven Maven Webapp

http://maven.apache.org

UTF-8

3.1.2.RELEASE

junit

junit

3.8.1

test

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-test

${spring.version}

javax.servlet

javax.servlet-api

3.0.1

provided

javax.servlet

jstl

1.1.2

provided

javax.servlet.jsp

javax.servlet.jsp-api

2.3.1

provided

org.codehaus.jackson

jackson-jaxrs

1.9.11

org.apache.commons

commons-lang3

3.3.2

commons-io

commons-io

2.4

org.apache.commons

commons-collections4

4.0

commons-logging

commons-logging

1.1.3

commons-codec

commons-codec

1.8

commons-beanutils

commons-beanutils

1.8.3

commons-chain

commons-chain

1.2

commons-fileupload

commons-fileupload

1.3.1

org.apache.commons

commons-math3

3.3

org.apache.commons

commons-pool2

2.2

org.apache.commons

commons-digester3

3.2

commons-net

commons-net

3.3

commons-dbutils

commons-dbutils

1.5

org.apache.commons

commons-email

1.3.3

commons-dbcp

commons-dbcp

1.4

jstl

jstl

1.2

taglibs

standard

1.1.2

log4j

log4j

1.2.16

org.slf4j

slf4j-api

1.7.5

com.google.code

kaptcha

2.3.0

mysql

mysql-connector-java

5.1.31

SpringMVC-Maven

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

cn.jxufe

SpringMVC-Maven

war

1.0

SpringMVC-Maven Maven Webapp

http://maven.apache.org

UTF-8

3.1.2.RELEASE

junit

junit

3.8.1

test

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-test

${spring.version}

javax.servlet

javax.servlet-api

3.0.1

provided

javax.servlet

jstl

1.1.2

provided

javax.servlet.jsp

javax.servlet.jsp-api

2.3.1

provided

org.codehaus.jackson

jackson-jaxrs

1.9.11

org.apache.commons

commons-lang3

3.3.2

commons-io

commons-io

2.4

org.apache.commons

commons-collections4

4.0

commons-logging

commons-logging

1.1.3

commons-codec

commons-codec

1.8

commons-beanutils

commons-beanutils

1.8.3

commons-chain

commons-chain

1.2

commons-fileupload

commons-fileupload

1.3.1

org.apache.commons

commons-math3

3.3

org.apache.commons

commons-pool2

2.2

org.apache.commons

commons-digester3

3.2

commons-net

commons-net

3.3

commons-dbutils

commons-dbutils

1.5

org.apache.commons

commons-email

1.3.3

commons-dbcp

commons-dbcp

1.4

jstl

jstl

1.2

taglibs

standard

1.1.2

log4j

log4j

1.2.16

org.slf4j

slf4j-api

1.7.5

com.google.code

kaptcha

2.3.0

mysql

mysql-connector-java

5.1.31

SpringMVC-Maven

12、完成以上步骤后,可以继续配置jdbc.properties、log4j.properties等文件。至此SpringMVC项目搭建完成,工程结构如下图:

13、代码我都会托管到GibHub上,有需要的朋友可以查看。github


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java序列化与ObjectOutputStream和ObjectInputStream的实例详解
下一篇:接口测试用例的编写(接口的测试用例一般都怎么写)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~