xml - 当我不使用全局命名空间时,为什么 web.xml 不起作用?

标签 xml tomcat jakarta-ee namespaces web.xml

我有这个非常简单的 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/

相关文章:

xml - 在 PowerShell 中替换 XML 节点

android - 处理 SAX 解析器异常

javascript - 在 Firefox 附加组件中对 XML 使用 XPath

java - 在 VM 上部署更多 Tomcat 实例是否有意义?

java - 正确传输和保护网络应用程序的用户密码

java - Keycloak 错误 token 响应,当用户不具有 Offline_access 角色时 error=not_allowed

java - 部署多个不同应用的war时,tomcat容器如何区分读取哪个war文件的web.xml

java - 这里如何使用Gson将列表发送回jsp页面?

Java 11 包 javax.xml.bind 不存在

android - 我正在尝试为空 ListView 制作一个 TextView,但我无法从 Activity 中获取 ListView