Eclipse complaining about web-app attributes

2020-05-22 19:36发布

My web-app declaration in my web.xml is:

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

And eclipse complains about all 4 attributes, here is one Eclipse complaint:

Attribute "version" must be declared for element type "web-app"

Why is Eclipse complaining about these attributes? Am I doing something wrong here?

7条回答
Explosion°爆炸
2楼-- · 2020-05-22 20:16

Didn't you forget this row in your xml file?:

    <?xml version="1.0" encoding="ISO-8859-1"?>

Your file should start like this:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

If it doesn't work clean and refresh your project.

查看更多
【Aperson】
3楼-- · 2020-05-22 20:22

Just remove the DOCTYPE. And its works fine.

查看更多
时光不老,我们不散
4楼-- · 2020-05-22 20:27

I had the same problem. I tried a different schema location and it worked for me. Instead of

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

try using this

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd

Also, set the version as "3.0".

查看更多
Rolldiameter
5楼-- · 2020-05-22 20:29

Remove the DOCTYPE line, that is what the xsd is meant to replace. I had the same issue and only this had worked.

查看更多
beautiful°
6楼-- · 2020-05-22 20:33

Refer to this oracle.com link to know what all were announced with Java EE 7 (assuming that is what we're working with). The link states

All new schemas are in the http://xmlns.jcp.org/xml/ns/javaee/ namespace.

The namespace java.sun has not become java.oracle, instead it has been retained by The Java Community Process (JCP). Hence all namespaces should point to xmlns.jcp.org, as is also pointed out by Java EE 7 docs.

Now, when we are deploying a web application (and hence the need to define a web.xml), first we have to know what we intend to do.

Say, we are working with Apache Tomcat V8. If we read the docs here, we get to know that it comes with Servlet 3.1 bundled. So, I would prefer an implementation of Servlet 3.1, just to stay up to date if not anything else.

Now all that being said, this is the declaration I would have

<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_3_1.xsd"
         version="3.1">

</web-app>

Hope this answers the question.

查看更多
何必那么认真
7楼-- · 2020-05-22 20:38

I changed the declaration from

http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd">

to the following and it worked like charm. Now no errors shown in web.xml

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

查看更多
登录 后发表回答