Java 为什么在 Eclipse 中创建 Servlet 会破坏我的 web.xml?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4008010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is creating Servlets in Eclipse breaking my web.xml?
提问by Brian Kessler
Being somewhat lazy, I was rather happy to find that I could create a new servlet source code by going New -> Servlet, instead of going New -> Class and then editing the class into a servlet.
有点懒,我很高兴地发现我可以通过转到 New -> Servlet 创建一个新的 servlet 源代码,而不是转到 New -> Class 然后将类编辑到 servlet 中。
However, I have discovered that every time I create a new servlet in Eclipse, Eclipse modifies my web.xml.
但是,我发现每次在 Eclipse 中创建新的 servlet 时,Eclipse 都会修改我的 web.xml。
Specifically, it modifies the top element to:
具体来说,它将顶部元素修改为:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
(Linebreaks mine.)
(我的换行符。)
This doesn't seem necessarily bad, but then it modifies various sub-elements by putting "javaee:" in front of their name, to indicate that these elements belong in that namespace.
这似乎并不一定是坏事,但是它通过在其名称前面放置“javaee:”来修改各种子元素,以指示这些元素属于该命名空间。
For example, it changes
例如,它改变
<display-name>ShowLifecycles</display-name>
to
到
<javaee:display-name>ShowLifecycles</javaee:display-name>
After which eclipse then complains about all the elements it modified, giving me notations like:
之后 eclipse 会抱怨它修改的所有元素,给我这样的符号:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'javaee:display-name'. One of '{"http://
java.sun.com/xml/ns/j2ee":description, "http://java.sun.com/xml/ns/j2ee":display-name, "http://java.sun.com/xml/ns/
j2ee":icon, "http://java.sun.com/xml/ns/j2ee":distributable, "http://java.sun.com/xml/ns/j2ee":context-param, "http://
java.sun.com/xml/ns/j2ee":filter, "http://java.sun.com/xml/ns/j2ee":filter-mapping, "http://java.sun.com/xml/ns/
j2ee":listener, "http://java.sun.com/xml/ns/j2ee":servlet, "http://java.sun.com/xml/ns/j2ee":servlet-mapping, "http://
java.sun.com/xml/ns/j2ee":session-config, "http://java.sun.com/xml/ns/j2ee":mime-mapping, "http://java.sun.com/xml/ns/
j2ee":welcome-file-list, "http://java.sun.com/xml/ns/j2ee":error-page, "http://java.sun.com/xml/ns/j2ee":jsp-config, "http://
java.sun.com/xml/ns/j2ee":security-constraint, "http://java.sun.com/xml/ns/j2ee":login-config, "http://java.sun.com/xml/ns/
j2ee":security-role, "http://java.sun.com/xml/ns/j2ee":env-entry, "http://java.sun.com/xml/ns/j2ee":ejb-ref, "http://
java.sun.com/xml/ns/j2ee":ejb-local-ref, "http://java.sun.com/xml/ns/j2ee":service-ref, "http://java.sun.com/xml/ns/
j2ee":resource-ref, "http://java.sun.com/xml/ns/j2ee":resource-env-ref, "http://java.sun.com/xml/ns/j2ee":message-
destination-ref, "http://java.sun.com/xml/ns/j2ee":message-destination, "http://java.sun.com/xml/ns/j2ee":locale-
encoding-mapping-list}' is expected.
To make matters worse, when I use find and replace to delete all to "javaee:" which litters the file, Eclipse still complains about these even though they are no longer there. I must copy and paste the entire remaining file on top of itself to make these complaints go away.
更糟糕的是,当我使用 find 和 replace 将所有内容都删除到“javaee:”文件时,Eclipse 仍然抱怨这些,即使它们不再存在。我必须将剩余的整个文件复制并粘贴到自身之上,以使这些投诉消失。
I am sure Eclipse is trying to be useful, anticipating some desire or need for this namespace. How can I do either one of two things:
我确信 Eclipse 正在努力发挥作用,期待对此命名空间的一些愿望或需求。我该如何做两件事之一:
Make it stop doing this?
Take advantage of whatever it is trying to do, and make it work for me instead of against me?
让它停止这样做?
利用它试图做的任何事情,让它为我工作而不是反对我?
采纳答案by BalusC
I have never seen this before, but this indicates that your Eclipse project is really messed up. At least the web.xml
root declaration makes no utter sense. It look like a mix of Servlet 2.4 and 2.5 specifications. Maybe Eclipse is getting confused because the root namespace (xmlns
) is pointing to the Servlet 2.4 one (with j2ee
URI) while the web project itself is set as Servlet 2.5 or newer (which should be using the one with javaee
URI).
我以前从未见过这种情况,但这表明您的 Eclipse 项目真的一团糟。至少web.xml
根声明没有完全意义。它看起来像是 Servlet 2.4 和 2.5 规范的混合体。也许 Eclipse 会感到困惑,因为根命名空间 ( xmlns
) 指向 Servlet 2.4(带有j2ee
URI),而 Web 项目本身被设置为 Servlet 2.5 或更新版本(应该使用带有javaee
URI 的那个)。
Also, when your web project is set to Servlet 3.0 during creation, by defaultno web.xml
will be generated by Eclipse because of the new Servlet 3.0 annotations like @WebServlet
, @WebFilter
, etc.. which makes the web.xml
superfluous. When you create new servlets by New > Servlet, Eclipse will already autogenerate those annotations. Probably you've attempted to create the web.xml
yourself based on misinformation.
此外,在创建过程中,您的Web项目设置为Servlet的3.0,在默认情况下没有web.xml
将Eclipse生成,因为新的Servlet 3.0批注喜欢的@WebServlet
,@WebFilter
等等。这使得web.xml
多余的。当您通过New > Servlet创建新的 servlet 时,Eclipse 将自动生成这些注释。可能您已经尝试web.xml
根据错误信息创建自己。
I'd suggest to backup some code if necessary, throw the whole project away and create a new one with the proper settings and do not touch the web.xml
root declaration.
如有必要,我建议备份一些代码,将整个项目扔掉并使用适当的设置创建一个新项目,并且不要触及web.xml
根声明。
Assuming that you're using the latest Eclipse version, Helios SR1 for Java EE developers, rightclick Eclipse's Project Explorer, choose New > Dynamic Web Projectand just fill the project name and keep everything default. Click Nextuntil the last step and then tick Generate web.xml deployment descriptorcheckbox to let Eclipse generate one. The root declaration should then look like this:
假设您使用的是最新的 Eclipse 版本 Helios SR1 for Java EE 开发人员,右键单击 Eclipse 的Project Explorer,选择New > Dynamic Web Project并填写项目名称并保留所有默认值。单击Next直到最后一步,然后勾选Generate web.xml 部署描述符复选框,让 Eclipse 生成一个。根声明应如下所示:
<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_3_0.xsd"
id="WebApp_ID" version="3.0">
回答by Ed Pike
I was having a similar issue. I brought a web app over from an old Tomcat 6/Java 6 to Tomcat 7/Java 7. I copy and pasted existing web.xml body into the new 3.0 web.xml. Everything was fine until I added a servlet that had init-param, display-name, description and load-on-startup paramters. Eclipse flagged these params as broken with an "invalid content found at...". Thanks to stackoverflow, I found that load-on-startup had to come after init-params but only a RTFM for how they knew. My other params were still flagged as broken. This is what the web.xml header looked like when it was broken:
我遇到了类似的问题。我将一个 Web 应用程序从旧的 Tomcat 6/Java 6 带到了 Tomcat 7/Java 7。我将现有的 web.xml 主体复制并粘贴到新的 3.0 web.xml 中。一切都很好,直到我添加了一个具有 init-param、display-name、description 和 load-on-startup 参数的 servlet。Eclipse 将这些参数标记为“在...处发现无效内容”。多亏了 stackoverflow,我发现 load-on-startup 必须在 init-params 之后,但只有一个 RTFM 才能知道他们是如何知道的。我的其他参数仍然被标记为损坏。这是 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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
I changed the dtds to ns/j2ee and web-app_2_5.xsd, and Ecliupse stopped complaining. But its 2014 and we'll be moving to Tomcat 8 soon. So I dug in hard and found the docs for . In web-app 3.0 the parameters have to be in this order:
我将 dtds 更改为 ns/j2ee 和 web-app_2_5.xsd,Ecliupse 停止抱怨。但它是 2014 年,我们很快就会转向 Tomcat 8。所以我努力挖掘并找到了 . 在 web-app 3.0 中,参数必须按以下顺序排列:
- jee:descriptionGroup = any combo of 3 params: description, display-name, icon
- servlet-name
- chose 1: servlet-class or jsp-file
- n number of init-params
- load-on-startup
- options: enabled, asynch-supported, run-as, security-role-ref, multipart-config
- jee:descriptionGroup = 3 个参数的任意组合:描述、显示名称、图标
- servlet 名称
- 选择 1:servlet-class 或 jsp-file
- n 初始化参数的数量
- 启动时加载
- 选项:启用、异步支持、运行方式、安全角色引用、多部分配置
Once I put my params in order, Eclipse was happy with ns/javaee and web-app_3_0.xsd.
一旦我整理好参数,Eclipse 就对 ns/javaee 和 web-app_3_0.xsd 感到满意。
回答by Lucky
Make sure the web.xml
file starts with the tag followed by the tag
确保web.xml
文件以标签开头,后跟标签
<?xml version="1.0" encoding="UTF-8"?>
In my case I had DOCTYPE tag and removing the DOCTYPE tag from the web.xml cleared me this error,
在我的情况下,我有 DOCTYPE 标记并从 web.xml 中删除 DOCTYPE 标记清除了我这个错误,
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
回答by Vayu
Move "display-name" as the first element under "servlet" tag, the validation error should go away.
将“显示名称”作为“servlet”标签下的第一个元素,验证错误应该消失。