首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Maven 手動創建現代化 Web 項目:告別舊版 Archetype 坑點

     編輯:Java 綜合教學

默認 archetype 痛點

  • 依賴 JDK 1.5,過時
  • Dynamic Web Module 2.3,不支援 Servlet 3.0+ 註解
  • 缺少 Servlet/JSP/JSTL 依賴,JSP 報錯 The superclass "javax.servlet.http.HttpServlet" was not found

手動創建標準流程

1. 創建 Simple Project(跳過 Archetype 選擇)

2. 填寫 GAV 坐標

3. 修正 pom.xml 核心配置

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- Servlet 3.1 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!-- JSP 2.2 -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <!-- JSTL 1.2 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- SLF4J + Log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.7</version>
    </dependency>
</dependencies>

4. 手動創建 Web 結構

  • src/main/webapp/WEB-INF/web.xml(Servlet 3.0 版本)
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
</web-app>

5. 修正項目 Facet 版本

切換到 Navigator 視圖,編輯 .settings/org.eclipse.wst.common.project.facet.core.xml:

<installed facet="jst.web" version="3.0"/>

6. 更新 Maven 項目

Alt + F5 → 勾選 Force Update of Snapshots/Releases → OK

最終效果

  • JDK 1.7+ 編譯
  • Servlet 3.0 支援註解配置
  • JSP/JSTL/SLF4J 開箱即用
  • 無紅叉報錯
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved