How to get an image or js from a JAR in the server in JSF (For a custom component)

做~自己de王妃 提交于 2019-12-25 15:56:10

问题


I have done a custom JSF component (it looks like a combobox (icefaces selectonemenu)) but it uses a couple of images (png) and a bit of javascript. I jar everything, so then the developers use it as a jar copied in the web-inf/lib folder. The image and the js are just for this custom component, so I can't make them put this image and js in his project, it has to be in MY jar.

I jar everything and it works almost great, just the image and the JS, I do not get them to work. I do not know how to reference them being in the jar. I could make them work as long as they are part of the application, but not being part of the jar. How should I do to get them in my encodebegin code for example?

I am using JSF with icefaces 1.8

Thanks in advance!!


回答1:


If you're already on JSF 2.0, it should work just fine out the box when you're using @ResourceDependency or @ResourceDependencies annotation which can resolve resources based on JAR's /META-INF/resources folder.

On JSF 1.x, your best bet is to create a custom "resource servlet" which is mapped on a certain URL pattern, e.g. /com.nahiko.resources/* and just streams the resources from the classpath to the HTTP response. Basic kickoff example:

String path = "/META-INF/resources" + request.getPathInfo();
InputStream input = getClass().getResourceAsStream(path);
OutputStream output = response.getOutputStream();
// ...

Document along your JAR that this servlet has to be mapped. Or if you target a Servlet 3.0 container, just add the @WebServlet annotation to get it to auto-register.



来源:https://stackoverflow.com/questions/6908089/how-to-get-an-image-or-js-from-a-jar-in-the-server-in-jsf-for-a-custom-componen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!