1

I am using a iframe for displaying image on the page. iframe is making action call on page load and action is returning fileinputstream. My question is if file is not available then action is breaking up and throwing error on the page. So, how would i handle that error if resource is not available on the server.

Way I am making action call through iframe is

$('#imgLogo').attr('src', '../employer/DisplayLogoOnLoad.action');

And my action looks like

companyLogo = getLogoPath();  
companyLogo = getClass().getClassLoader().getResource("../../images/" + companyLogo)
      .getPath();  
    inputStream = new FileInputStream(new File(companyLogo));  
    return SUCCESS;

Please suggest me the way to handle iframe image if null resource.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
ankit
  • 438
  • 10
  • 26

2 Answers2

0

Don't return a success result, return something that renders something appropriate.

Alternatively, create a default image to stream back if the requested image isn't found.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Thanks Dave for the replies, I handled it by creating a new default image and send back to stream. But still it is showing error on the UI like 'Image contains errors'. – ankit Jan 08 '13 at 07:53
  • Yes, image contained errors. Therefore it is not showing on the UI. – ankit Jan 10 '13 at 04:42
  • @ankit It's unclear to me if you've fixed it. That aside, you could also do something on the jQuery side if you don't want to stream a default image (or generated image), e.g., instead of using an image tag, send back HTML and replace a DOM element, use an error code and handle it using success/error callbacks, etc. – Dave Newton Jan 10 '13 at 16:00
0

don't believe that there is a way without 2 requests: ajax to check if resource exists(if true then return path to the resource) if not exits then don't show image or show error msg(no path is returned and second request is not executed)

Zemzela
  • 3,060
  • 6
  • 23
  • 32
  • I don't understand why it wouldn't be possible to return something reasonable if the file doesn't exist, can you elaborate? People check for the existence of resources all the time. – Dave Newton Jan 08 '13 at 12:42
  • sure it is possible to return something reasonable if the file doesn't exists, and ofcourse, should be done in that way – Zemzela Jan 10 '13 at 13:20
  • I'm asking why it would require two requests. – Dave Newton Jan 10 '13 at 14:04
  • how you will know that resource is not available ? – Zemzela Jan 10 '13 at 15:42
  • Did you see his code? You can check for the existence of a resource. – Dave Newton Jan 10 '13 at 15:54
  • @Dave... If result type of action="stream" then we can not return something reasonable. We have make 2 request, 1st request will check whether resource exist or not. If resource exist, then call action which return stream type and If resource doesnt exist, then will call action which return something else other than stream(in my case I am returning json). – ankit Jan 11 '13 at 07:40
  • @ankit tnx for clarifying my toughs, exactly what I wanted to say. – Zemzela Jan 11 '13 at 13:20
  • and @DaveNewton before down vote make sure that you are right – Zemzela Jan 17 '13 at 08:59
  • @Zemzela How about a default, or generated image, as I suggested? How is that *possibly* wrong? – Dave Newton Jan 17 '13 at 11:20
  • @ankit How about a default, or generated image, as I suggested? It's ridiculous to believe that Java code can only take one path: it has conditionals, and they can be used. That you returned an invalid image isn't Java's fault, nor mine. – Dave Newton Jan 17 '13 at 11:21
  • @DaveNewton well... I didn't say your is totally wrong, its somehow a solution(but doesn't corespondent to question), and also didn't down vote your answer because I don't like. I respect your opinion as you should respect someone else opinion. Mine is also not wrong. – Zemzela Jan 17 '13 at 14:19
  • @Zemzela It corresponds *precisely* to the question, which was how to handle the error if the resource isn't found: **avoid the error in the first place**. I never said your solution wouldn't *work*, I said that two requests aren't *required*. If you're going to make an Ajax request *anyway*, why bother with *two*? Use an error handler instead. – Dave Newton Jan 17 '13 at 14:24
  • @DaveNewton.... When I created default file of type png and then UI shows error like "Image contains error". I guess it was because I am converting image to 64bytes and then returning it to UI and when I am trying to convert empty data file into 64bytes. then it must be throwing some kind of error. – ankit Jan 18 '13 at 09:58