struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kawczynski, David" <david_kawczyn...@merck.com>
Subject RE: Displaying an image in JSP in struts+tiles project
Date Tue, 05 Jan 2010 16:07:53 GMT
Being a fan of the KISS policy... I played around with 
this but didn't have the time to figure it out, so I 
made the URL spat out by struts2 points to a servlet 
instead of struts2.  

(Note that my web.xml has struts2 configured to listen 
for *.action rather then slash-star)

Good luck & please let me know what your solution is.

Thanks!
-dave


> -----Original Message-----
> From: Aruna Ponaka [mailto:aruna.hcu@gmail.com] 
> Sent: Monday, January 04, 2010 5:09 PM
> To: user@struts.apache.org
> Subject: Displaying an image in JSP in struts+tiles project
> 
> 
> Hi,
> 
> Am building an application in struts2 and tiles. My requirement is to
> retrieve an image blob from mysql database and display the 
> image in a jsp
> using img tag as below..
> 
> img src="<s:url action="myAction"/>" 
> 
> Part of my struts.xml is as below:
> 
>  <package name="default" extends="struts-default">
>         <result-types>
>             <result-type name="tiles"
> class="org.apache.struts2.views.tiles.TilesResult" />
>             <result-type name="myBytesResult"
> class="com.icensa.action.MyBytesResult" ></result-type><br>
>         </result-types>
>          <action name="myAction" class="com.icensa.action.MyAction">
>                <result name="myImageResult" type="myBytesResult" /> 
>          </action>
> </package>
> My MyBytesResult class is:
> 
> public class MyBytesResult implements Result {
> 
> 	private static final long serialVersionUID = 1L;
> 	
> 
> 	public void execute(ActionInvocation invocation) throws 
> Exception {
> 		
> 		MyAction action = (MyAction) invocation.getAction();
> 		HttpServletResponse response = 
> ServletActionContext.getResponse();
> 
>         response.setContentType("image/jpeg");
> 		
> //response.setContentLength(action.getMyContentLength());
> 
> 		
> response.getOutputStream().write(action.getMyImageInBytes());
> 		response.getOutputStream().flush();
> 	}
> 
> }
> 
> And MyAction class is:
> 
> public class MyAction extends ActionSupport {
> 	
> 	private static final long serialVersionUID = 1L;
> 	Blob image = null;
>     Connection con = null;
>     Statement stmt = null;
>     ResultSet rs = null;
>     byte[] imgData = null;
>     OutputStream o = null;
>     HttpServletResponse response = ServletActionContext.getResponse();
> 	public String doDefault() {
> 		System.out.println("doDefault()");
> 		try {
> 		      Class.forName("com.mysql.jdbc.Driver");
> 		      con =
> DriverManager.getConnection("jdbc:mysql://localhost:3306/proje
> ct","root","pass");
> 		      stmt = con.createStatement();
> 		      rs = stmt.executeQuery("select * from 
> books_tb where
> category='General' order by publish_date desc");
> 		      while (rs.next()) {
> 			        image = rs.getBlob(11);
> 			        imgData = 
> image.getBytes(1,(int)image.length());		         
> 			 } 
> 		   }
> 		catch (Exception e) {
> 		          System.out.println(e.getMessage());	
>          
> 		    } 
> 	    return "myImageResult";
> 	  }
> 
> 	  public byte[] getMyImageInBytes() { 
> 		  System.out.println("getMyImageInBytes()");
> 		  try{
> 		  
> 		  }
> 		  catch (Exception e) {
> 	          System.out.println(e.getMessage());
> 	         
> 	     } 
> 		  return imgData;
> 	  }
> 
> 	//  public String getMyContentType() { }
> 	//  public String getMyContentDisposition() {}
> 	//  public int getMyContentLength() { }
> 	//  public int getMyBufferSize() { }
> 
> 	}
> 
> when i run the code the image is not displayed and I get an 
> error saying
> No result defined for action com.action.MyAction and result success
> 
> In struts.xml is I provide reslut name="success" and the jsp, 
> it does not
> throw an error but not displaying the image. However in any 
> case it is not
> going to MyAction.java. What could i do to rectify this?
> Could provide you with required code if necessary..
> 
> Thanks,
> Aruna 
> 
> -- 
> View this message in context: 
> http://old.nabble.com/Displaying-an-image-in-JSP-in-struts%2Bt
iles-project-tp27020146p27020146.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
Notice:  This e-mail message, together with any attachments, contains information of Merck
& Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates
Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html)
that may be confidential, proprietary copyrighted and/or legally privileged. It is intended
solely for the use of the individual or entity named on this message. If you are not the intended
recipient, and have received this message in error, please notify us immediately by reply
e-mail and then delete it from your system.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message