将共享代码和 web.xml 从 WAR 项目拆分为公共 JAR 项目 [英] Splitting up shared code and web.xml from WAR project to common JAR project
问题描述
我有不同的 web 应用程序,它们共享大部分 web.xml 配置.例如,某些 servlet 的映射方式对于所有应用程序都是相同的,但某些 Web 应用程序具有自定义 servlet、或附加过滤器或共享托管 bean.
I have different webapps that share a large portion of the web.xml configuration. For example, the way some servlets are mapped is identical for all apps, but some webapps have custom servlets, or an additional filter, or shared managed beans.
在我的 IDE 中,每个 web 应用程序都是不同的项目.我想将我的 web.xml 的公共部分重构为公共"项目,并让所有特定于应用程序的 web.xml 扩展公共"web.xml.有没有可能做到这一点?
Each webapp is a different project in my IDE. I would like to refactor the common part of my web.xml to a "common" project, and let all the application-specific web.xml's extend the "common" web.xml. Is there any possibility to do this?
推荐答案
是的,你可以.假设 Eclipse(您的问题历史确认您正在使用它),只需创建一个Web Fragment Project":
Yes, you can. Assuming Eclipse (your question history confirms that you're using it), just create a "Web Fragment Project":
并将其与向导中的主项目关联起来:
And associate it with the main project in the wizard:
如有必要,您可以(重新)在主 Web 项目的部署程序集"属性中配置它(或任何其他配置构建的方式,最终以 /WEB-INF 中的 JAR 结束/lib
主网络项目).
You can if necessary (re)configure it in "Deployment Assembly" property of the main web project (or any other way of configuring the build in such way that it ultimately ends up as JAR in /WEB-INF/lib
of main web project).
它基本上是一个具有以下文件夹结构的 Java 项目:
It's basically a Java project with the following folder structure:
CommonWebProject
|-- com.example... (you can put e.g. @WebServlet classes here)
|
|-- META-INF
| |-- resources (you can put shared web resources here)
| | |-- common.css
| | |-- common.js
| | |-- template.jsp
| | :
| |
| |-- beans.xml
| |-- web-fragment.xml
| `-- MANIFEST.MF
:
/META-INF/resources
文件夹中的任何内容都以与 WAR 的 webcontent 相同的方式解析(我的 Eclipse Luna SR1 没有预先创建 /resources
文件夹,因此您需要手动创建它).而且,重要的是,WAR 中已经存在的任何同名资源(包括类!)都将优先于 JAR 中的资源,因此您可以在必要时以这种方式覆盖"来自 WAR 的公共 JAR 资源.
Any content in /META-INF/resources
folder is resolved the same way as webcontent of the WAR (my Eclipse Luna SR1 didn't precreate the /resources
folder, so you'd need to manually create it). And, importantingly, any resources (including classes!) with the same name already in the WAR will have loading precedence over those in JAR, so you could if necessary "override" a common JAR resource from WAR on that way.
请注意,web-fragment.xml
文件必须完全如此命名,而不是 web.xml
.IDE 应该只是为您自动生成一个,但为了完整起见,一个 Servlet 3.0 兼容的看起来像这样:
Note that the web-fragment.xml
file must be named exactly like that and thus not web.xml
. The IDE should just autogenerate one for you, but for sake of completeness, a Servlet 3.0 compatible one look like this:
<?xml version="1.0" encoding="utf-8"?>
<web-fragment
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-fragment_3_0.xsd"
version="3.0"
>
<!-- Put shared web.xml config here. -->
</web-fragment>
另见:
- 具有共享代码的多个 JSF 项目的结构(如果您使用的是 JSF)
- Structure for multiple JSF projects with shared code (in case you're using JSF)
See also:
这篇关于将共享代码和 web.xml 从 WAR 项目拆分为公共 JAR 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!