8.Eclipse下创建Maven的WEB项目

 

Eclipse下创建Maven的WEB项目

1. 创建项目

Fixed: 解决maven archetypes为空的问题

Preferences中去掉Packaged: Maven Central的勾选

Fixed: 解决src/main/java不显示问题

项目右键-Properties

2. 引入相关的jar

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
<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.web</groupId>
<artifactId>maven-web</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>maven-web Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.2.1</jsp.version>
<jstl.version>1.1.2</jstl.version>
</properties>

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

<!-- jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.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>

</dependencies>

<!-- 项目构建节点 -->
<build>
<finalName>maven-web</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>
<!-- tomcat插件的请求端口 -->
<port>8080</port>
<!-- 项目的请求路径 -->
<path>/hello</path>
</configuration>
</plugin>
</plugins>
</build>

</project>

功能: 在index.jsp里面输出1-100

Fixed: 修改jsp页面

原有的index.jsp页面非常简单,删除原有页面,新建一个index.jsp页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach begin="1" end="100" var="num">
${num }
</c:forEach>
</body>
</html>

Fixed: 修改web.xml版本

2.3 –> 3.1

原有的web.xml时2.3版本的,而2.3的版本不支持注解和文件上传的注解

删除原有的src/main/webapp/下的web.xml

右键项目—Properties—Project Facets

Java版本选择1.8

然后Dynamic Web Module,先去掉勾选,然后选择3.1版本,点击Apply,然后再勾选上

选择Further configuration available

Content directory输入: src/main/webapp,勾选Generate web.xml deployment descriptor

3. 运行项目

项目右键-Run As-Run Configurations

tomcat的maven plugin只支持7

Goals: 输入tomcat7:run

点击Run

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
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.web:maven-web >--------------------------
[INFO] Building maven-web Maven Webapp 1.0
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ maven-web >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-web ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ maven-web <<<
[INFO]
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ maven-web ---
[INFO] Running war on http://localhost:8080/hello
[INFO] Using existing Tomcat server configuration at E:\SpringToolSuite\project\demo-4.8.1\maven-web\target\tomcat
[INFO] create webapp with contextPath: /hello
十二月 30, 2020 11:31:21 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
十二月 30, 2020 11:31:21 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
十二月 30, 2020 11:31:21 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
十二月 30, 2020 11:31:22 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]