我可以从 lib 中的 JAR 内部提供 JSP,还是有解决方法? [英] Can I serve JSPs from inside a JAR in lib, or is there a workaround?
问题描述
我在 Tomcat 7 中部署了一个作为 WAR 文件的 Web 应用程序.该应用程序构建为一个多模块项目:
I have a web application deployed as a WAR file in Tomcat 7. The application is build as a multi-module project:
- core - 打包为 JAR,包含大部分后端代码
- core-api - 打包为 JAR,包含面向核心的接口
- webapp - 打包为 WAR,包含前端代码并依赖于核心
- customer-extensions - 可选模块,打包为 JAR
通常,我们可以将我们的 JSP 文件放在 webapp 项目中,并相对于上下文引用它们:
Normally, we can put our JSP files in the webapp project, and reference them relative to the context:
/WEB-INF/jsp/someMagicalPage.jsp
问题是我们如何处理特定于客户扩展项目的 JSP 文件,这些文件不应该总是包含在 WAR 中.不幸的是,我不能在 JAR 文件中引用 JSP,它出现了.尝试 classpath:jsp/customerMagicalPage.jsp
会导致在 JspServlet 中找不到文件,因为它使用了 ServletContext.getResource()
.
The question is what we do about JSP files that are specific to the customer-extensions project, that should not always be included in the WAR. Unfortunately, I cannot refer to JSPs inside JAR files, it appears. Attempting classpath:jsp/customerMagicalPage.jsp
results in a file not found in the JspServlet, since it uses ServletContext.getResource()
.
传统上,我们通过 maven 解压客户扩展 JAR、定位 JSP 并在构建时将它们放入 WAR 来解决"这个问题.但理想的情况是,您只需将 JAR 放入 Tomcat 中分解的 WAR 中,然后就会发现该扩展——这适用于除 JSP 之外的所有内容.
Traditionally, we "solved" this having maven unpack the customer-extensions JAR, locate the JSPs, and put them in the WAR when building it. But an ideal situation is where you just drop a JAR in the exploded WAR in Tomcat and the extension is discovered - which works for everything but the JSPs.
有没有办法解决这个问题?标准方式、特定于 Tomcat 的方式、黑客或解决方法?例如,我一直在考虑在应用程序启动时解压 JSP...
Is there anyway to solve this? A standard way, a Tomcat-specific way, a hack, or a workaround? For example, I've been thinking of unpacking the JSPs on application startup...
推荐答案
Tomcat 7 支持的 Servlet 3.0 包括将 jsps 打包成 jar 的能力.
Servlet 3.0 which Tomcat 7 supports includes the ability to package jsps into a jar.
您需要:
- 将你的 jsps 放在你的 jar 的
META-INF/resources
目录中 - 可选择在 jar 的
META-INF
目录中包含一个web-fragment.xml
- 将 jar 放在你的战争的
WEB-INF/lib
目录中
- place your jsps in
META-INF/resources
directory of your jar - optionally include a
web-fragment.xml
in theMETA-INF
directory of your jar - place the jar in
WEB-INF/lib
directory of your war
然后您应该能够在您的上下文中引用您的 jsps.例如,如果您有一个 jsp META-INF/resources/test.jsp
,您应该能够在上下文的根目录中将其引用为 test.jsp
You should then be able to reference your jsps in your context. For example if you have a jsp META-INF/resources/test.jsp
you should be able reference this at the root of your context as test.jsp
这篇关于我可以从 lib 中的 JAR 内部提供 JSP,还是有解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!