eclipse中使用maven创建java web工程的实现方式
1)在eclipse项目栏中右键单击空白,new(或直接使用ctrl+n快捷键) —— other ——maven project。
2)选择以webapp模板创建工程
3)填写group id 、 artifact id 等信息。
groupid
定义了项目属于哪个组,举个例子,如果你的公司是mycom,有一个项目为myapp,那么groupid就应该是com.mycom.myapp.
artifacted
定义了当前maven项目在组中唯一的id,比如,myapp-util,myapp-domain,myapp-web等。
version
指定了myapp项目的当前版本,snapshot意为快照,说明该项目还处于开发中,是不稳定的版本。
4)创建完成之后,有的电脑会提示servelet相关的包没有被导入。这个我们可以统一在pom.xml文件中统一去写。pom.xml里面的依赖可以在 http://mvnrepository.com/ 这个网站上去查找,该站点已经帮你把依赖按格式写好,十分方便。下面给出一份我以前做练手项目所用的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
|
<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>org.seckill</groupid>
<artifactid>seckill</artifactid>
<packaging>war</packaging>
<version> 0.0 . 1 -snapshot</version>
<name>seckill maven webapp</name>
<url>http: //maven.apache.org</url>
<dependencies>
<dependency>
<!-- 使用junit4注解方式 -->
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version> 4.11 </version>
<scope>test</scope>
</dependency>
<!-- 补全项目依赖 -->
<!-- 1 :日志 java日志:slf4,log4j,logback slf4j是 规范、接口 日志实现:log4j、logback -->
<dependency>
<groupid>org.slf4j</groupid>
<artifactid>slf4j-api</artifactid>
<version> 1.7 . 12 </version>
</dependency>
<dependency>
<groupid>ch.qos.logback</groupid>
<artifactid>logback-core</artifactid>
<version> 1.1 . 1 </version>
</dependency>
<dependency>
<groupid>ch.qos.logback</groupid>
<artifactid>logback-classic</artifactid>
<version> 1.1 . 1 </version>
</dependency>
<!-- 实现slf4j接口并整合 -->
<!-- 2 :数据库相关依赖 -->
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version> 5.1 . 35 </version>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupid>c3p0</groupid>
<artifactid>c3p0</artifactid>
<version> 0.9 . 1.2 </version>
</dependency>
<!-- dao框架依赖:mybatis依赖 -->
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version> 3.3 . 0 </version>
</dependency>
<!-- mybatis自身实现的spring整合依赖 -->
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis-spring</artifactid>
<version> 1.2 . 3 </version>
</dependency>
<!-- 3 :servlet web相关依赖 -->
<dependency>
<groupid>taglibs</groupid>
<artifactid>standard</artifactid>
<version> 1.1 . 2 </version>
</dependency>
<dependency>
<groupid>jstl</groupid>
<artifactid>jstl</artifactid>
<version> 1.2 </version>
</dependency>
<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version> 2.5 . 4 </version>
</dependency>
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>javax.servlet-api</artifactid>
<version> 3.1 . 0 </version>
</dependency>
<!-- 4 :spring依赖 -->
<!-- 1 )spring核心依赖 -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-beans</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<!-- 2 )spring dao层 -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-jdbc</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-tx</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<!-- 3 )spring web依赖 -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-web</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<!-- 4 )spring test相关的依赖 -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-test</artifactid>
<version> 4.1 . 7 .release</version>
</dependency>
<!-- redis客户端:jedis -->
<dependency>
<groupid>redis.clients</groupid>
<artifactid>jedis</artifactid>
<version> 2.7 . 3 </version>
</dependency>
<!-- protostuff序列化 -->
<dependency>
<groupid>com.dyuproject.protostuff</groupid>
<artifactid>protostuff-core</artifactid>
<version> 1.0 . 8 </version>
</dependency>
<dependency>
<groupid>com.dyuproject.protostuff</groupid>
<artifactid>protostuff-runtime</artifactid>
<version> 1.0 . 8 </version>
</dependency>
<!-- collections集合框架工具 -->
<dependency>
<groupid>commons-collections</groupid>
<artifactid>commons-collections</artifactid>
<version> 3.2 . 1 </version>
</dependency>
</dependencies>
<build>
<finalname>seckill</finalname>
</build>
</project>
|
5)注意web.xml中要开启标签库和el表达式支持,要用2.3以上版本,所以把xml头部的都改掉
1
2
3
4
|
<web-app xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http: //xmlns.jcp.org/xml/ns/javaee
http: //xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version= "3.1" metadata-complete= "true" >
|
6)maven 项目的src/main/java 和 src/main/resources 都是项目根目录(classpath:)。例如,想访问src/main/resources 目录下的mybatis-config.xml,它的路径参数要写成(“mybatis-config.xml”)。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/kaka0509/article/details/68951657