0

Is there anyway to add resources to a web application using JSF programmatically? This would involve adding files in the resource folder (or any of it subfolders). I want to add a picture that can be treated as a resource so I display it with

<h:graphicImage name="name of the resource i create" library="subfolder under resources" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
arg20
  • 4,893
  • 1
  • 50
  • 74

1 Answers1

2

You don't want to write to web content programmatically. It will all get lost whenever you redeploy the webapp.

Just save it to disk or DB using FileOutputStream or PreparedStatement#setBinaryStream() and then have a servlet which gets an InputStream of it from disk or DB using FileInputStream or ResultSet#getBinaryStream() respectively and then writes it to the OutputStream of the response along a proper set of HTTP response headers. Finally just call that servlet by its URL, along with the unique resource identifier or filename as request parameter or pathinfo.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC I am aware of that, but it is so much more complicated to create a dedicated servlet and load images from there. Specially when iterating through collections. Also, I can't really save it to disk that would impact scalability if I plan on distributing my application – arg20 Mar 23 '11 at 19:29
  • I was planning on creating resources dinamically and then doing a backup, since redeploying is not something i do everyday in production – arg20 Mar 23 '11 at 19:30
  • I don't see how that's more complicated when iterating through collections. Even more, I don't see anything which needs to be changed for that. As to scalability, just use a DB then. It was just an answer to put you in the right direction. Furthermore, you may find some useful insights in this answer: http://stackoverflow.com/questions/2340406/retrieve-multiple-images-from-mysql/2341322#2341322 Just substitute `` with ``. – BalusC Mar 23 '11 at 19:35
  • @BalusC What I was trying to do was treat all content with a uniform approach, using JSF resourcehandler etc. May be I misused the term complicated. What I meant was that I wanted to treat all resources dynamically generated and statics in the same way. So doing for instance would retrieve matt's picture. is that not a good idea? – arg20 Mar 23 '11 at 19:46
  • I don't see how that's possible with JSF resource management since it resolves resources from the classpath or webcontent. It has to be included in the webapp deployment already. JSF resource management is also more meant for resources which are specific to JSF components, such as specific background images, styles and/or scripts and not for user-uploaded data or something. – BalusC Mar 23 '11 at 19:52
  • I see, I apologize then. I had understood resource management wrong. Thanks for your help! – arg20 Mar 23 '11 at 19:58