简体   繁体   English

我可以从 web-fragment.xml 中引用 web.xml 中的 servlet 或过滤器吗?

[英]Can I refer a servlet or filter in web.xml from web-fragment.xml?

So I have a web.xml that defines a servlet or a filter that has some servlet-mapping or filter-mapping associated with it, then I want to add some more servlet-mapping/filter-mapping entries in web-fragment.xml, will that work? So I have a web.xml that defines a servlet or a filter that has some servlet-mapping or filter-mapping associated with it, then I want to add some more servlet-mapping/filter-mapping entries in web-fragment.xml,那行得通吗? Say web.xml:说 web.xml:

<servlet>
<servlet-name>A</servlet-name>
<servlet-class>com.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>A</servlet-name>
<url-pattern>/Path1</url-pattern>
</servlet-mapping>

then in web-fragment.xml I have然后在 web-fragment.xml 我有

<servlet-mapping>
<servlet-name>A</servlet-name>
<url-pattern>/Path2</url-pattern>
</servlet-mapping>

From my testing it doesn't work but I don't understand why would it be so.从我的测试来看,它不起作用,但我不明白为什么会这样。

I experienced this same issue.我遇到了同样的问题。 I believe the answer is that he 3.1 servlet spec says我相信答案是他3.1 servlet 规范

<servlet-mapping> elements with the same <servlet-name> are additive across web-fragments.具有相同 <servlet-name> 的 <servlet-mapping> 元素在 web-fragments 中是相加的。 <servlet-mapping> specified in the web.xml overrides values specified in the web-fragments with the same <servlet-name>在 web.xml 中指定的 <servlet-mapping> 会覆盖在具有相同 <servlet-name> 的 web-fragments 中指定的值

(And something analogous for filter-mappings.) (以及类似于过滤器映射的东西。)

They are additive only across web-fragments , not across both the web.xml and the web-fragments .它们仅在 web-fragments中添加,而不是在 web.xml 和 web-fragments中添加。 If they occur in the web.xml, they override all the ones on the fragments.如果它们出现在 web.xml 中,它们会覆盖片段上的所有内容。 This seemed to be born out by my testing with Tomcat using the logEffectiveWebXml feature.这似乎是通过我使用 logEffectiveWebXml 功能对 Tomcat 进行的测试得出的。

So you could make this work by moving these mapping out of the web.xml into a web-fragment.xml.因此,您可以通过将这些映射从 web.xml 移出到 web-fragment.xml 中来完成这项工作。 (In my a case, I decided to abandon the web-fragments approach as it became too hard to manage the ordering across multiple files.) (在我的例子中,我决定放弃 web-fragments 方法,因为管理多个文件的排序变得太难了。)

As of Servlet 3.0 Specification web-fragment is introduced for for pluggability of library jars which are packaged under WEB-INF/lib. Servlet 3.0规范开始,引入了Web片段以实现可打包的WEB-INF / lib下的库jar的可插入性。 The content of web.xml and web-fragment.xml are almost the same. web.xml和web-fragment.xml的内容几乎相同。

I guess you are not linking your fragments to web.xml, so probably you have to do something like this: 我猜您没有将片段链接到web.xml,因此可能必须执行以下操作:

 <absolute-ordering>
    <name>fragment3</name>
    <name>fragment2</name>
    <name>fragment1</name>
    <others/>
    ...
</absolute-ordering>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM