新建一個Maven項目詳細步驟及Maven中的問題和bug超全解

新建一個Maven的Web項目詳細步驟及Maven中的問題和bug全解

新建一個Maven項目詳細步驟

1. 創建Web項目

File —》 New —》Maven Project

新建Maven的Web項目
下一步:

在這裏插入圖片描述

下一步:

Maven項目中的座標(GAV):就可以找到、定位到唯一的項目
Group Id :表示包名
Artifact Id :項目名
Version : 版本號

在這裏插入圖片描述

2. 1新建的項目就發現 jsp 有紅叉,有錯誤

========================= 錯誤 ================================

錯誤如下:
Description Resource Path Location Type
The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
index.jsp /web/src/main/webapp line 1 JSP Problem

解決辦法: 引入相關jar包: 在pom.xml 中配置依賴包
在刪了index.jsp,在新建一個即可

<dependencies>
		<!--javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<!-- jsp-api -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

在這裏插入圖片描述

2. 2新建的項目就發現有紅叉,少src/main/java 和 src/test/java

在這裏插入圖片描述

在這裏插入圖片描述
====================== 解決辦法 解決src/main/java不顯示問題==============

在這裏插入圖片描述

3. 運行項目之配置tomcat的運行插件

也是在 pom.xml 中配置, 在 中配置

<build>
		<finalName>03_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> <!--解決頁面提交數據亂碼問題 -->
					<port>8080</port><!-- tomcat插件的請求端口 -->
					<path>/項目名</path><!-- 項目的請求路徑 -->
				</configuration>
			</plugin>
		</plugins>
</build>

注意:
1. tomcat端口號和項目名需要更改成自己的
2. maven項目只支持到tomcat7,之後的還沒麼得

4. 運行項目

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述
==================== 訪問 項目 (127.0.0.1/8888/項目名)=====================

4. 補充: 有JSTL表達式時

在 pom.xml 導入依賴包,這裏展示完整的 pom.xml 配置

		<!-- 添加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>

================================= 完整 =============================

	<!--配置版本號,好處:需要修改版本,只改這裏一次即可 -->
	<properties>
		<servlet.version>3.1.0</servlet.version>
		<jsp.version>2.2</jsp.version>
		<jstl.version>1.1.2</jstl.version>
	</properties>
	
	<dependencies>
		<!--javax.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>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>03_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> <!--解決頁面提交數據亂碼問題 -->
					<port>8080</port><!-- tomcat插件的請求端口 -->
					<path>/hello</path><!-- 項目的請求路徑 -->
				</configuration>
			</plugin>
		</plugins>
	</build>

在這裏插入圖片描述

==================== 仍可能存在Servlet版本不匹配的問題 ===============================
在這裏插入圖片描述
============================ 解決方法 ===============================

1,刪除web.xml
2,右鍵項目—properties—project faects
在這裏插入圖片描述

3,去掉Dynamic Web Module前面的勾選
在這裏插入圖片描述

4,更改2.3—3.1,然後應用
在這裏插入圖片描述
在這裏插入圖片描述
5,再把Dynamic Web Moule勾上,7,點擊Further進入
在這裏插入圖片描述
在這裏插入圖片描述

6,查看web.xml ,發現神奇的事情發生了,版本號改爲了3.1

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章