问题描述
我正在尝试按照模块化原则构建一个java Web应用程序,在一个WAR中使用一些公共资源(主要是JSP),在另一个WAR中使用一些自定义资源。这意味着JSP分散在不同的WAR中。
I'm trying to construct a java web app along modular principles, with some common resources (JSPs, mainly) in one WAR, and some custom resources in another. This means JSPs scattered across different WARs.
现在,JavaEE对这种恶作剧感到皱眉,并且希望你把所有东西放在一个地方。我目前的解决方法是使用Eclipse触发的Ant脚本将一个WAR的内容复制到另一个中,但这不是一个令人愉快的解决方案(它很脆弱且依赖于IDE)。
Now JavaEE frowns upon this sort of shenanigans, and wants you to put everything in one place. My current workaround to this is to have an Eclipse-triggered Ant script which copies the content of one WAR into the other, but this is not a pleasant solution (it's fragile and too IDE-dependent).
理想情况下,我希望能够做的是让servlet转发到位于不同WAR中的JSP,并将其部署到其中。这样可以让我更自由地组装WAR。但是, RequestDispatcher
似乎不支持这样的事情。
Ideally, what I'd like to be able to do is for a servlet to forward to a JSP located in a different WAR to one in which it is itself deployed. This would allow greater freedom in how I assemble my WARs. However, the RequestDispatcher
does not seem to support such things.
另一种可能性是使用< c:import>
, 允许从不同的WAR导入资源(有一些警告)。这可能允许我在一个WAR中有一个钩子JSP,然后从另一个JSP中拖入所需的JSP。但这有点笨拙,而< c:import>
允许它显示底层servlet API也是如此。但是如何通过servlet中的 RequestDispatcher
访问该功能?
Another possibility is to use <c:import>
, which does allow resources to be imported from a different WAR (with some caveats). This would probably allow me to have a "hook" JSP in one WAR, which then drags in the required JSP from another. This is a bit clunky, though, and the fact that <c:import>
permits it shows that the underlying servlet API does also. But how do I access that functionality via the RequestDispatcher
in a servlet?
推荐答案
您可以通过以下步骤:
-
使用
获取外国<$ c的 RequestDispatcher
$ c> ServletCotnext 。
get the RequestDispatcher
of the foreign ServletCotnext
.
in META-INF / context.xml
set crossContext =true
(可能是tomcat特定的)
in META-INF/context.xml
set crossContext="true"
(perhaps tomcat-specific)
这篇关于Servlet:转发到不同Web应用程序中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!