IDEA使用Maven創建項目時web.xml中的web-app報錯及版本約束問題

IDEA使用Maven創建項目時web.xml中的web-app報錯

以下是我創建maven項目時所遇到的錯誤,希望對大家有所幫助

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>

當我將上述代碼寫入之後,web-app和version顯示紅色報錯,百思不得其解,以上問題從兩方面分析:

1.從version版本上,可能你的EL表達式並不支持這個版本,可以查詢換個對應的低版本。
這個需要強調一點,servlet-api版本不一定要與web.xml中的版本相對應
2.web.xml配置有問題,試試下面的,(有時候可能會寫的少東西,具體少什麼不太確定,可以直接複製以下代碼)我修改爲下列代碼時,報錯已經全部解決

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
    <display-name>db</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

此時我pom.xml中的servlet-api的版本爲:

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

此時可以看出:pom.xml中的servlet-api的版本爲:4.0.1,web.xml中的版本爲2.5,

他們並不統一,但是運行項目不影響,此時我又將web.xml中的version改爲4.0版本,發現又報紅,對版本進行約束,此時又陷入困境,我估計有可能是我idea這個工具的版本比較低,因爲我的jdk是1.8,按理說他是支持4.0的,再試試下面的,發現4.0仍然報錯

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">

  <display-name>website name</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

爲什麼會報錯,

歡迎大家留言幫忙解答

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