struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aruna Ponaka <aruna....@gmail.com>
Subject RE: Displaying an image in JSP in struts+tiles project
Date Mon, 11 Jan 2010 20:15:51 GMT

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


Mime
View raw message