Maven Web 工程

本教程将教您如何在Eclipse中创建 Archetype为 maven-archetype-webapp的Maven项目,也就是web工程。

创建Maven工程

第一步,启动Eclipse,依次打开菜单【File】【New】【Other】

找到目录Maven,选择Maven Project,点击【Next】

点击【Next】

选择一个Archetype。这里创建Web工程,选择maven-archetype-webapp

点击【Next】

最后点击【Finish】,将生成一个maven项目。项目的图标左上角有一个m字母。

随后将创建工程。工程结构如下:

解决问题


创建后,报几个问题。

这样解决,打开pom.xml,添加以下东东。

1、添加插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< plugin >
     < groupId >org.apache.maven.plugins</ groupId >
     < artifactId >maven-war-plugin</ artifactId >
     < version >2.6</ version >
     < configuration >
         < failOnMissingWebXml >false</ failOnMissingWebXml >
     </ configuration >
</ plugin >
< plugin >
     < groupId >org.apache.maven.plugins</ groupId >
     < artifactId >maven-compiler-plugin</ artifactId >
     < version >3.1</ version >
     < configuration >
         < source >1.8</ source >
         < target >1.8</ target >
     </ configuration >
</ plugin >

2、添加serlvet依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< dependency >
     < groupId >javax.servlet</ groupId >
     < artifactId >javax.servlet-api</ artifactId >
     < version >3.1.0</ version >
</ dependency >
< dependency >
     < groupId >javax.servlet.jsp</ groupId >
     < artifactId >javax.servlet.jsp-api</ artifactId >
     < version >2.3.1</ version >
</ dependency >
< dependency >
     < groupId >javax.servlet</ groupId >
     < artifactId >jstl</ artifactId >
     < version >1.2</ version >
</ dependency >

完整的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
< project xmlns = "http://maven.apache.org/POM/4.0.0"
     < modelVersion >4.0.0</ modelVersion >
     < groupId >com.liyongzhen</ groupId >
     < artifactId >myweb</ artifactId >
     < packaging >war</ packaging >
     < version >0.0.1-SNAPSHOT</ version >
     < name >myweb Maven Webapp</ name >
     < url >http://maven.apache.org</ url >
     < dependencies >
         < dependency >
             < groupId >javax.servlet</ groupId >
             < artifactId >javax.servlet-api</ artifactId >
             < version >3.1.0</ version >
         </ dependency >
         < dependency >
             < groupId >javax.servlet.jsp</ groupId >
             < artifactId >javax.servlet.jsp-api</ artifactId >
             < version >2.3.1</ version >
         </ dependency >
         < dependency >
             < groupId >javax.servlet</ groupId >
             < artifactId >jstl</ artifactId >
             < version >1.2</ version >
         </ dependency >
         < dependency >
             < groupId >junit</ groupId >
             < artifactId >junit</ artifactId >
             < version >3.8.1</ version >
             < scope >test</ scope >
         </ dependency >
     </ dependencies >
     < build >
         < finalName >myweb</ finalName >
         < plugins >
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-war-plugin</ artifactId >
                 < version >2.6</ version >
                 < configuration >
                     < failOnMissingWebXml >false</ failOnMissingWebXml >
                 </ configuration >
             </ plugin >
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-compiler-plugin</ artifactId >
                 < version >3.1</ version >
                 < configuration >
                     < source >1.8</ source >
                     < target >1.8</ target >
                 </ configuration >
             </ plugin >
         </ plugins >
     </ build >
</ project >

3、更新这个工程

右击工程名,在弹出的菜单中选择【Maven】【Upadte Project】

点击【OK】

到此项目创建完成,可以运行。

右击工程名,在弹出的菜单中选择【Run As】【Run on Server】

点击【Next】

最后点击【Finish】

运行效果


调整Servlet版本

当前主流Servlet开发或者说主流Java Web开发,Servlet的版本是3.1。(Servlet 4.0还未被大量采用)。

我们在添加依赖时,Servlet版本也是3.1,而maven工程创建时Servlet是2.3

 

右击工程名,在弹出的菜单中选择【Properties】

弹出对话框,选择【Project Facets】

找到 Dynamice Web Module,去掉前面勾选

将Serlvet版本更改为3.1,再点击【Apply】

再将Dynamice Web Module勾选上去。

最后点击【Apply and Close】

再看,更新过来。


如果有web.xml,可能是这样的

1
2
3
4
5
6
7
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 
< web-app >
   < display-name >Archetype Created Web Application</ display-name >
</ web-app >

这是Servlet2.3。需要换成Servlet3.1命名空间。

下面是Servlet3.1命名空间。将web.xml改成下面一样的命名空间。

1
2
3
4
5
6
7
8
9
10
<? xml version = "1.0" encoding = "UTF-8" ?>
id = "WebApp_ID" version = "3.1" >
   < display-name >myweb</ display-name >
   < welcome-file-list >
     < welcome-file >index.jsp</ welcome-file >
   </ welcome-file-list >
</ web-app >

 

猜你喜欢

转载自www.cnblogs.com/max-hou/p/10953415.html