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

Eclipse 創建 Dynamic Web Project 未生成 web.xml:勾選生成選項或手動創建

     編輯:程式設計綜合問答

原因

創建動態 Web 項目時,向導最後一步有 **「Generate web.xml deployment descriptor」** 選項,默認**未勾選**(Eclipse 3.7+ / Servlet 3.0+ 支持註解配置,web.xml 非必需)。若未勾選,項目不會自動生成 `WEB-INF/web.xml`。

解決方法

  1. 重新創建項目:向導最後一步勾選「Generate web.xml deployment descriptor」。
  2. 手動創建:在 `WebContent/WEB-INF/` 右鍵 → New → Other → Web → **Web XML File**,Eclipse 會生成標準模板。
    模板內容:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
        xmlns="https://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee 
        https://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0">
      <display-name>MyWebApp</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
  3. 從其他項目複製:複製現有 `web.xml` 到 `WEB-INF/`,修改 `` 等項目特定配置。

Servlet 3.0+ 註解替代 web.xml

現代開發推薦**無 web.xml 純註解配置**:

  • `@WebServlet("/url")` 替代 `` ``。
  • `@WebFilter("/path")` 替代 `` ``。
  • `@WebListener` 替代 ``。
  • `ServletContainerInitializer` / `SpringServletContainerInitializer` 啟動 Spring。

僅在需兼容舊容器、複雜安全約束、JNDI 數據源等場景才需保留 `web.xml`。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved