gpt4 book ai didi

java - 用图像封装自定义jsp标签

转载 作者:行者123 更新时间:2023-12-01 05:31:09 30 4
gpt4 key购买 nike

我正在为客户做一些工作,他们有一个在各种项目中使用的通用 Web 标签 jar 文件项目。他们要求我制作一个页眉和页脚标签,他们可以在项目中使用类似的标签...

<pageElements:header/>

它的自定义标记方面很简单,我创建了一个标题标记,它提供了包含一些文本的样板 html 标记。

我遇到的问题是他们想要一些图像打包在 jar 中,用于 Logo 等内容。显然,如果我放一些类似的东西......

<img alt="logo" src="images/logo.png"/>

...在标签中,它正确呈现,但在使用图像标签的项目上下文中查找。它不存在,因为它位于包含标签定义的 jar 文件内的 resources/images 目录中。

有谁知道我可以将图像打包在包含标签定义的jar文件中,然后在标签中引用它,以便当项目使用标签时......

<pageElements:header/>

...它正确呈现文本和图像吗?

本质上,我需要知道的是图像应该保存在 jar 文件中的哪个位置,以及如何在定义标签内容的 tagx 文件中引用它们。

非常感谢,

标记

最佳答案

servlet 3.0 规范说:

The root of this hierarchy serves as the document root for files that are part of the application. For example, for a Web application with the context path /catalog in a Web container, the index.html file at the base of the Web application hierarchy or in a JAR file inside WEB-INF/lib that includes the index.html under META-INF/resources directory can be served to satisfy a request from /catalog/index.html. If an index.html is present both in the root context and in the META-INF/resources directory of a JAR file in the WEB-INF/lib directory of the application, then the file that is available in the root context MUST be used.

因此,您可以将图像放在 jar 中的 META-INF/resources 下,它们将像直接在 war 中一样被使用。

以前版本的规范不提供这种可能性,因此您必须使用 servlet 或过滤器来实现它。 Spring MVC 和 Struts2 本身就提供了这一点。我不知道其他框架。

关于java - 用图像封装自定义jsp标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8944975/

30 4 0