用图像封装自定义jsp标签

发布于 2024-12-28 11:39:54 字数 656 浏览 1 评论 0原文

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

<pageElements:header/>

它的自定义标签方面很简单,我创建了一个页眉标签,它提供了样板 html 标记,其中包含一些内容文本。

我遇到的问题是他们想要一些图像打包在罐子中,用于徽标等。显然,如果我在标签中放入类似...

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

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

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

<pageElements:header/>

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

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

非常感谢,

马克

I'm doing some work for a client and they have a common web tag jar file project that they use in various projects. They've asked me to make a header and footer tag that they can use in projects using a tag something like...

<pageElements:header/>

The custom tag aspect of it is simple and I have created a header tag which provides boiler plate html markup comprising of some text.

The problem I'm having is that they want some images packaged up in the jar for things like logos and so on. Obviously if I put something like...

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

...in the tag it's rendered correctly but looks in the context of the project that's using the tag for the image. It doesn't exist as it's in the resources/images directory within the jar file containing the definitions of the tags.

Does anyone know of a way I can package an image in the jar file containing the tag definitions and then refer to it in the tag so that when the project uses the tag...

<pageElements:header/>

...it renders the text and the images correctly?

Essentially what I need to know is where in the jar file should the images be kept and how do I refer to them in the tagx file defining the tag content.

Many thanks,

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2025-01-04 11:39:54

Servlet 3.0 规范指出:

此层次结构的根用作以下文件的文档根:
是应用程序的一部分。例如,对于一个 Web 应用程序
Web 容器中的上下文路径 /catalog,index.html 文件位于
Web 应用程序层次结构的基础或 JAR 文件中
WEB-INF/lib 包含 META-INF/resources 下的 index.html
可以提供目录以满足来自 /catalog/index.html 的请求。
如果index.html同时存在于根上下文和
WEB-INF/lib 中 JAR 文件的 META-INF/resources 目录
应用程序的目录,然后是可用的文件
必须使用根上下文。

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

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

The servlet 3.0 specification says:

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.

So you can place your images in your jar, under META-INF/resources, and they will be served as if they were directly in the war.

Previous versions of the spec don't provide this possibility, so you'll have to use a servlet or filter to implement it. Spring MVC and Struts2 provide this natively. I don't know about other frameworks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文