1. 程式人生 > >web.xml檔案報錯

web.xml檔案報錯

提示資訊應該能看懂。也就是缺少了web.xml檔案,被設定成true了。
搜尋了一下,Stack Overflow上的答案解決了問題,分享一下。
目前被頂次數最多的回答原文如下:
This is a maven error. It says that it is expecting a web.xml file in your project because it is a web application, as indicated by war. However, for recent web applications a web.xml file is totally optional. Maven needs to catch up to this convention. Add this to your maven pom.xml to let maven catch up and you don’t need to add a useless web.xml to your project:
大意是說這是一個Maven錯誤,在最近的web應用開發中web.xml檔案已經變得可有可無了。不過Maven還沒有跟上這一變化,我們只要在pom.xml檔案中手動新增如下配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml
>
false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>

“告知”maven即可。這樣做的好處是我們不必在專案生中成一個無用的web.xml檔案。在更新版本(具體從哪一個版本開始我也不知道~~)的maven中已經不存在web.xml檔案缺失的問題,我們只需要處理被設定成tue的問題即可。也就是在pom.xml中新增如下配置即可。

<properties>
    <failOnMissingWebXml
>
false</failOnMissingWebXml> </properties>

其他方案:(生成web.xml——>部署執行時檔案路徑)
you can do it also like this:

Right click on Deployment Descriptor in Project Explorer.
Select Generate Deployment Descriptor Stub.
It will generate WEB-INF folder in src/main/webapp and an web.xml in it.

這是被採納方案,也就是說在專案瀏覽器中右鍵單擊Deloyment Descriptor,選擇生成部署描述符存根:Generate Deployment Descriptor Stub。

操作後會在專案的src/main/webapp/WEB-INF目錄下生成web.xml檔案。我按照這個方法生成了web.xml檔案,但是仍然報錯。如果你也是的話可以接著往下看。
Here are the steps you can follow:

You can check this by right clicking your project, and open its Properties dialogue, and then “Deployment Assembly”, where you can add Folder /src/main/webapp. Save the setting, and then,
Go to Eclipse menu Project -> Clean… and clean the project, and the error should go away.
此時需要對src/main/webapp檔案進行執行時的路徑部署,以保證伺服器正常執行。
也就是說需要部署一下src/main/webapp(剛生成的web.xml就在該資料夾下)資料夾,Deployment的作用是會在執行時將相應的resource複製到web伺服器的相應目錄下(通過Deploy Path指定),保證web應用正常執行。
經過這兩步,問題解決。