我有这个非常简单的 web.xml 文件:
<?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_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>testx.jsp</welcome-file>
</welcome-file-list>
</web-app>
当我部署应用程序并访问上下文根时,我将被带到 testx.jsp,这很好。但在我的 web.xml 文件中,我不想使用全局命名空间,因此我按如下方式更改 web.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<ee:web-app xmlns:ee="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">
<ee:welcome-file-list>
<ee:welcome-file>testx.jsp</ee:welcome-file>
</ee:welcome-file-list>
</ee:web-app>
现在,当我再次启动 tomcat 并访问该页面时,我会发现自己位于/index 而不是/testx.jsp。但为什么?
我正在将应用程序部署到 Tomcat 服务器。我尝试了Glassfish,没有遇到这个问题。我猜这是 Tomcat 的问题?
最佳答案
您的 XML 正确且有效,但是 xmlNamespaceAware
Tomcat Web 应用程序 Context 的属性可能设置为 false(默认)。
我能够使用 web.xml 重现您所描述的行为,该 web.xml 使用元素的命名空间前缀,就像您的示例一样。修改%CATALINA_HOME%/conf/context.xml
后将 xmlNamespaceAware 属性添加为 true <Context xmlNamespaceAware="true">
,welcome-file-list 的行为符合预期。
https://tomcat.apache.org/tomcat-7.0-doc/config/context.html
xmlNamespaceAware
If the value of this flag is true, the parsing of web.xml and web-fragment.xml files for this web application will be namespace-aware. Note that *.tld, *.jspx and *.tagx files are always parsed using a namespace-aware parser and that the tagPlugins.xml file (if any) is never parsed using a namespace-aware parser. Note also that if you turn this flag on, you should probably also turn xmlValidation on. If the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true, the default value of this attribute will be true, else the default value will be false. Setting this attribute to true will incur a performance penalty.
关于xml - 当我不使用全局命名空间时,为什么 web.xml 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26699855/