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-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to