服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用Maven搭建SpringMVC项目的步骤(图文教程)

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

2021-01-08 11:50何海洋 Java教程

本篇文章主要介绍了使用Maven搭建SpringMVC项目的步骤(图文教程),非常具有实用价值,需要的朋友可以参考下

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

  1.eclipse

  2.maven

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

1、新建一个maven project

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

 2、选择工作空间

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

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

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

 4、填写项目参数,如图

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

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

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

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

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

 7、确保勾选上dynamic web module和java

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" 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_2_5.xsd">
  
  <!-- 配置web.xml,使其具有springmvc特性,主要配置两处,一个是contextloaderlistener,一个是dispatcherservlet -->
  
  <context-param>
    <param-name>contextconfiglocation</param-name>
    <param-value>classpath*:applicationcontext.xml</param-value>
  </context-param>
  <!-- 配置contextloaderlistener表示,该工程要以spring的方式启动。启动时会默认在/web-inf目录下查找applicationcontext.xml
作为spring容器的配置文件,该文件里可以初始化一些bean -->
  <listener>
    <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
  </listener>
  
  <!-- 字符集过滤器 -->
  <filter>
    <filter-name>encodingfilter</filter-name>
    <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingfilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- spring mvc 配置 -->
  <!-- 配置dispatcherservlet表示,该工程将采用springmvc的方式。启动时也会默认在/web-inf目录下查找xxx-servlet.xml作为配置文件,
    xxx就是dispatcherservlet的名字,该文件中将配置两项重要的mvc特性:handlermapping,负责为dispatcherservlet这个前端控制器的请求查找controller;
    viewresolver,负责为dispatcherservlet查找modelandview的视图解析器。
    此处使用指定的配置文件spring-mvc.xml -->
  <servlet>
    <servlet-name>contacts</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
    <init-param>
      <param-name>contextconfiglocation</param-name>
      <!--<param-value>/web-inf/classes/spring-mvc-servlet.xml</param-value>-->
      <param-value>classpath*:/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>contacts</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
 
  <!-- bean definitions here -->
  <context:component-scan base-package="cn.jxufe" />
  
  <!-- 配置读取外部配置文件 -->
  <context:property-placeholder location="classpath:jdbc.properties" />
  <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"
    destroy-method="close" p:driverclassname="${jdbc.driverclassname}"
    p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
  <context:annotation-config /> <!-- 此行语句使得resource autowired 等四个注解可以使用 -->
  
  <!-- 实例化jdbctemplate,同时注入数据源 -->
  <bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate"
    p:datasource-ref="datasource" />
  
  <!-- 配置事务管理器 -->
  <bean id="txmanager"
    class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
    <property name="datasource" ref="datasource" />
  </bean>
  
  <!-- 事务扫描开始(开启@tranctional) -->
  <tx:annotation-driven transaction-manager="txmanager" />
 
</beans>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd"
default-lazy-init="true">
 
  <!-- 启动事务添加配置 -->
  <context:component-scan base-package="cn.jxufe">
    <!-- 排除扫描service标签 -->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.service"/> 
  </context:component-scan>
  
  <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter">
    <!-- 注册jason转换器 -->
    <property name="messageconverters">
      <list>
        <ref bean="mappingjacksonhttpmessageconverter"/>
        <ref bean="utf8stringhttpmessageconverter"/>
      </list>
    </property>
  </bean>
  
  <bean id="mappingjacksonhttpmessageconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter">
    <property name="supportedmediatypes">
      <list>
        <value>text/plain;charset=utf-8</value>
        <value>text/html;charset=utf-8</value>
        <value>application/json;charset=utf-8</value>
      </list>
    </property>
  </bean>
  <bean id="utf8stringhttpmessageconverter" class="org.springframework.http.converter.stringhttpmessageconverter">
    <property name="supportedmediatypes">
      <list>
        <value>text/plain;charset=utf-8</value>
        <value>text/html;charset=utf-8</value>
        <value>application/json;charset=utf-8</value>
      </list>
    </property>
  </bean>
 
  <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver">
    <property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/>
    <property name="prefix" value="/web-inf/pages/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
 
  <bean class="org.springframework.web.servlet.i18n.localechangeinterceptor"></bean>
 
  
  <!-- 当配置了<mvc:annotation-driven/>后,会自动加载org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping和
org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter,所以如果显示的配置则需去掉配置<mvc:annotation-driven/> -->
  <!--<mvc:annotation-driven/>-->
</beans>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<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>cn.jxufe</groupid>
  <artifactid>springmvc-maven</artifactid>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>springmvc-maven maven webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <spring.version>3.1.2.release</spring.version>
  </properties>
 
  <dependencies>
 
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
    <!-- spring begin -->
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-webmvc</artifactid>
      <version>${spring.version}</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-jdbc</artifactid>
      <version>${spring.version}</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context</artifactid>
      <version>${spring.version}</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-aop</artifactid>
      <version>${spring.version}</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-core</artifactid>
      <version>${spring.version}</version>
    </dependency>
 
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-test</artifactid>
      <version>${spring.version}</version>
    </dependency>
    <!-- spring end -->
 
    <!-- web jar -->
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>javax.servlet-api</artifactid>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>jstl</artifactid>
      <version>1.1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>javax.servlet.jsp</groupid>
      <artifactid>javax.servlet.jsp-api</artifactid>
      <version>2.3.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- jackson jar -->
    <dependency>
      <groupid>org.codehaus.jackson</groupid>
      <artifactid>jackson-jaxrs</artifactid>
      <version>1.9.11</version>
    </dependency>
 
    <!-- commons jar -->
    <dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-lang3</artifactid>
      <version>3.3.2</version>
    </dependency>
    <dependency>
      <groupid>commons-io</groupid>
      <artifactid>commons-io</artifactid>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-collections4</artifactid>
      <version>4.0</version>
    </dependency>
    <dependency>
      <groupid>commons-logging</groupid>
      <artifactid>commons-logging</artifactid>
      <version>1.1.3</version>
    </dependency>
    <dependency>
      <groupid>commons-codec</groupid>
      <artifactid>commons-codec</artifactid>
      <version>1.8</version>
    </dependency>
    <dependency>
      <groupid>commons-beanutils</groupid>
      <artifactid>commons-beanutils</artifactid>
      <version>1.8.3</version>
    </dependency>
    <dependency>
      <groupid>commons-chain</groupid>
      <artifactid>commons-chain</artifactid>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupid>commons-fileupload</groupid>
      <artifactid>commons-fileupload</artifactid>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-math3</artifactid>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-pool2</artifactid>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-digester3</artifactid>
      <version>3.2</version>
    </dependency>
    <dependency>
      <groupid>commons-net</groupid>
      <artifactid>commons-net</artifactid>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupid>commons-dbutils</groupid>
      <artifactid>commons-dbutils</artifactid>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-email</artifactid>
      <version>1.3.3</version>
    </dependency>
    <dependency>
      <groupid>commons-dbcp</groupid>
      <artifactid>commons-dbcp</artifactid>
      <version>1.4</version>
    </dependency>
 
    <!-- jstl jar -->
    <dependency>
      <groupid>jstl</groupid>
      <artifactid>jstl</artifactid>
      <version>1.2</version>
    </dependency>
 
    <dependency>
      <groupid>taglibs</groupid>
      <artifactid>standard</artifactid>
      <version>1.1.2</version>
    </dependency>
    <!-- 日志相关 -->
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>1.2.16</version>
    </dependency>
 
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-api</artifactid>
      <version>1.7.5</version>
    </dependency>
 
    <!-- 验证码kaptcha -->
    <dependency>
      <groupid>com.google.code</groupid>
      <artifactid>kaptcha</artifactid>
      <version>2.3.0</version>
    </dependency>
    <!-- mysql -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.31</version>
    </dependency>
  </dependencies>
  <build>
    <finalname>springmvc-maven</finalname>
  </build>
</project>

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/hehaiyang/p/4578488.html

延伸 · 阅读

精彩推荐