You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Aruna Ponaka <ar...@gmail.com> on 2010/01/04 23:09:05 UTC

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/project","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%2Btiles-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


RE: Displaying an image in JSP in struts+tiles project

Posted by Aruna Ponaka <ar...@gmail.com>.
I wrote an action class and it worked. 
I called the action class like below...

img src="<s:url action="imgAction"></s:url>

and the struts.xml 

         <action name="imgAction" class="com.icensa.action.ImageAction">             
               <result type="tiles">books</result>
        </action> 


ImageAction.java


import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ImageAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private byte[] img=null;
	private int id = 0;
	Blob image = null;
       Connection con = null;
       Statement stmt = null;
       ResultSet rs = null;
       byte[] imgData = null;
       OutputStream o = null;
       HttpServletResponse response = ServletActionContext.getResponse();
   
	public String execute() {
		try {
			    Class.forName("com.mysql.jdbc.Driver");
		      con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","test");
		      stmt = con.createStatement();
		      rs = stmt.executeQuery("select * from books_tb where
category='Social and Economic' order by publish_date desc");
		      
		      while (rs.next()) {
			        image = rs.getBlob(10);
			        imgData = image.getBytes(1,(int)image.length());		         
			  } 
		      
		        response.setContentType("image/jpeg");
				System.out.println("id   "+getId());
		        OutputStream out = response.getOutputStream();
		       out.write(imgData);
		        out.close();
		        return null;
        }
		catch (Exception e) {
		          System.out.println(e.getMessage());	         
		    } 
		return null;
	  }


I was using doDefault function earlier and was trying to return the
outputstream, but that will not work and it should return null.

Hope this is useful to someone....

-- 
View this message in context: http://old.nabble.com/Displaying-an-image-in-JSP-in-struts%2Btiles-project-tp27020146p27116930.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


RE: Displaying an image in JSP in struts+tiles project

Posted by "Kawczynski, David" <da...@merck.com>.
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