2014-11-21 60 views
0

我正在为我的项目使用strust2和瓷砖。这里是用于输入ID的代码(jsp)。如何在jsp中显示来自mysql的检索图像。我正在使用struts2和瓷砖

<s:textfield name="uniqueID" label="Enter Unique ID" required="required"/> 

这是操作文件。

public String execute() throws Exception{ 
    SearchDao.search(selectedID,uniqueID); 
    return SUCCESS; 
} 

将检索图像。

try{ 
    . 
    . 
    . 
    rs = ps.executeQuery(); 

    if(rs.next()){   
     InputStream originalImageStream = rs.getBinaryStream(1); 

     File file = new File("Retina"+".jpg"); 
     FileOutputStream fileOutputStream = new FileOutputStream(file); 

     while ((length = originalImageStream.read()) != -1) { 
      fileOutputStream.write(length);     
     } 
     fileOutputStream.close(); 
    } 
    else{ 
     return null; 
    } 
} 
catch(Exception ex){ 
    ex.printStackTrace(); 
} 
return "found"; 

一旦图像发现,这将返回发现等操作文件将返回成功。在struts.xml中代码是

<action name="search"  class="com.ActionClasses.SearchAction"> 
    <result name="success" type="tiles"> found  </result> 
    <result name="input" type="tiles"> search  </result> 
    <result name="error" type="tiles"> notFound </result> 
</action> 

这里是tiles.xml文件。

<definition name="found" extends="home"> 
    <put-attribute name="myTitle" value="searchSuccess"/> 
    <put-attribute name="myBody" value="/found.jsp"/> 
</definition> 

现在我怎样才能显示found.jsp检索到的图像。我在互联网上发现了一些解决方案,但仅适用于使用struts2或struts2以及休眠的项目。我没有找到使用strus2和tile的项目的解决方案。谁能帮我。谢谢。

+0

我的问题是如何保存的图像中_bean_和_jsp_显示呢? – Pawan 2014-11-21 08:08:56

+0

使用您找到的解决方案之一。在这种情况下,瓷砖是无关紧要的。 – 2014-11-21 10:03:11

回答

0

尝试了这一点...我已经实现了类似的情景在我的项目之一,但在春天,我想这应该为你工作,几乎改变

这是我实施了。我创建了一个控制器(在Struts又名行动)像下面

@RequestMapping(value = "view", method = RequestMethod.GET) 
    public void getImage(@RequestParam(value = "location") String location, HttpServletResponse response) { 
     String ext = FilenameUtils.getExtension(location); 
     response.setContentType("image/"+ext); 
     try { 
      File file = new File(location); 
      BufferedImage image = ImageIO.read(file); 
      OutputStream baos = response.getOutputStream(); 
      ImageIO.write(image, ext, baos); 
      baos.flush(); 
      baos.close(); 
     } catch (FileNotFoundException e) { 
      logger.error("File Not Found: " + location, e); 
     } catch (IOException e) { 
      logger.error("Can't read the File: " + location, e); 
     } catch(Exception e) { 
      logger.error("Can't read input file: " + location, e); 
     } 

     return; 
    } 

然后在控制器/操作该服务器的实际看我做了这样的事情

final String IMAGE_RESOLVER = "../../image/view?location="; 
    Game game = gameService.populateGame(gameId); 
    if(game.getThumbnailPath() != null) { 
     game.setThumbnailPath(IMAGE_RESOLVER.concat(game.getThumbnailPath())); 
    } 
    mv.addObject("game", game); // set object to return to view 

当名称为“thumbnail”的DOM对象接收到该字符串时,它会调用上述提到的操作Ë它返回它的图像

希望这对你的作品