简体   繁体   中英

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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