9.使用maven重构springmvc的项目

 

使用maven重构springmvc的项目

1. 新建Maven Project

2. 复制文件

目录结构对应关系:

SpringMVC project Maven project
src src/main/java
resources src/main/resources
WebRoot src/main/webapp

文件对应关系:

SpringMVC project Maven project
src/* src/main/java/*
resources/* src/main/resources/*
WebRoot/resources/* src/main/webapp/resources/*
WebRoot/WEB-INF/view/* src/main/webapp/WEB-INF/view/*
WebRoot/WEB-INF/web.xml src/main/webapp/WEB-INF/web.xml
WebRoot/index.jsp src/main/webapp/index.jsp

3. 修改pom.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<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>com.test.mavenproject</groupId>
<artifactId>mavenproject</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>mavenproject Maven Webapp</name>
<url>http://maven.apache.org</url>

<!-- jar包版本声明 -->
<properties>
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.1.2</jstl.version>
<mybatis.version>3.5.2</mybatis.version>
<mybatis-spring.version>1.3.3</mybatis-spring.version>
<spring.version>4.3.24.RELEASE</spring.version>
<druid.version>1.1.20</druid.version>
<mysql.version>5.1.47</mysql.version>
<jackson.version>2.9.9</jackson.version>
<fileupload.version>1.3.1</fileupload.version>
<pagehelper.version>5.1.8</pagehelper.version>
<!-- 注意只能使用2.0以下的版本 -->
<log4j.version>1.2.17</log4j.version>
<hutool.version>4.6.10</hutool.version>
<fastjson.version>1.2.60</fastjson.version>
<zxing.version>3.4.0</zxing.version>
<poi.version>4.1.0</poi.version>
<collection4.version>4.3</collection4.version>
<validation.version>2.0.1.Final</validation.version>
<hibernate.validator.version>5.4.0.Final</hibernate.validator.version>
</properties>

<dependencies>
<!-- 导入jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- 导入servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>

<!-- 添加jstl的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${jstl.version}</version>
</dependency>

<!-- pagepelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- 导入spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>

<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${fileupload.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${zxing.version}</version>
</dependency>
<!--javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${zxing.version}</version>
</dependency>
<!-- poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>

<!-- commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${collection4.version}</version>
</dependency>
<!--validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation.version}</version>
</dependency>
<!-- hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>

<!--解决启动时el/ElManger的异常 -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
<exclusions>
<exclusion>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>


</dependencies>

<build>
<finalName>mavenproject</finalName>

<!-- 配置插件 -->
<plugins>
<plugin>
<!--maven的tomcat插件 -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding> <!--解决页面提交数据乱码问题 -->
<port>8080</port><!-- tomcat插件的请求端口 -->
<path>/mavenproject</path><!-- 项目的请求路径 -->
</configuration>
</plugin>
</plugins>

</build>
</project>

4. Eclipse Tomcat运行Maven项目

将项目部署至Eclipse Tomcat,启动项目

访问地址: http://localhost:8080/mavenproject

查看项目访问url:

Maven项目右键->Properties

Web Project Settings -> Context root: [项目名]

Servers -> server.xml -> <Context path="[项目名]" />

这两个地方的[项目名]是一致的

5. Tomcat 7 插件运行Maven项目

项目右键->Run As->Run Configurations…

Maven Build -> [项目名] -> Main -> Goals: tomcat7:run

访问地址: http://localhost:8080/mavenproject