简体   繁体   English

用图像封装自定义jsp标记

[英]Encapsulating custom jsp tag with images

I'm doing some work for a client and they have a common web tag jar file project that they use in various projects. 我正在为客户做一些工作,他们有一个通用的Web标签jar文件项目,可在各种项目中使用。 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. 它的自定义标签方面很简单,我创建了一个标题标签,该标题标签提供了包含一些文本的样板html标记。

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. 它不存在,因为它位于包含标记定义的jar文件内的resources / images目录中。

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... 有谁知道我可以将图像包装在包含标签定义的jar文件中,然后在标签中引用它的方式,以便在项目使用标签时...

<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. 本质上,我需要知道的是jar文件中应将图像保存在哪里,以及如何在定义标签内容的tagx文件中引用它们。

Many thanks, 非常感谢,

Mark 标记

The servlet 3.0 specification says: 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. 例如,对于在Web容器中具有上下文路径/ catalog的Web应用程序,该index.html文件位于Web应用程序层次结构的基础上,或者位于WEB-INF / lib内部的JAR文件中,该文件包含META下的index.html可以提供-INF / resources目录来满足/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. 如果在应用程序的WEB-INF / lib目录的JAR文件的根上下文中和META-INF / resources目录中都存在index.html,则必须使用根上下文中可用的文件。

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. 因此,您可以将图像放在META-INF / resources下的jar中,就像直接在战争中一样被使用。

Previous versions of the spec don't provide this possibility, so you'll have to use a servlet or filter to implement it. 规范的早期版本没有提供这种可能性,因此您必须使用servlet或过滤器来实现它。 Spring MVC and Struts2 provide this natively. Spring MVC和Struts2本机提供此功能。 I don't know about other frameworks. 我对其他框架一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM