-
Nov 28th, 2009, 08:02 PM
#1
image on jsp via spring MODEL object
Hi,
Here is my code which I was able to render some data(BLOB from mysql) to the success view i.e. JSP using spring. I want to see the image, is there a better way of coding in controller so once I get the image I can provide additional styles to it via tags on jsp. I have got it working via JDBC+SERVLET+HTML to display the image on browser but was thinking to use the MVC and am trying to understand the benefits of using spring in this context .
Controller file(image_controller.java)
import org.springframework.web.servlet.mvc.Controller ;
import org.springframework.web.servlet.ModelAndView ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpServlet ;
import javax.servlet.http.HttpServlet ;
import com.mysql.jdbc.Driver ;
import java.sql.* ;
import java.io.* ;
public class image_controller implements Controller
{
public ModelAndView handleRequest(HttpServletRequest request , HttpServletResponse response) throws Exception
{
Class.forName("com.mysql.jdbc.Driver") ;
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abcd","root","adminadmin") ;
Statement cs = con.createStatement() ;
ResultSet rs = cs.executeQuery("select image from img") ;
String imgBin = null ;
if(rs.next())
{
imgBin = rs.getString(1) ;
}
int len = imgBin.length() ;
byte[] rb = new byte[len] ;
InputStream stream = rs.getBinaryStream(1) ;
stream.read(rb,0,len) ;
rb = rs.getBytes(1) ;
return new ModelAndView("image","rb",rb) ;
}
}
showImg-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns
="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id = "viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
p
refix = "/WEB-INF/jsp/" p:suffix=".jsp" />
<bean name="/showImg.htm" class="image_controller" />
</beans>
image.jsp
<%@ page import="java.io.* ,java.util.* " %>
<%@ page language="java" contentType="image/jpeg" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<c:forEach var="da" items="${rb}">
${da}
</c:forEach>
With above code I am able to see image.jsp rendered with data as below
-1
-40
-1
-32
0
16
.
.
etc .. this seems to be the binary for the image . could anyone please help to convert it to image ,preferably if possible in controller itself . I am trying ,not to use the 'response' object and instead use Model Object as response object is what is being used ,when Servlet+JDBC is used without spring. Any Help is appreciated.
-
Nov 29th, 2009, 04:32 AM
#2
If you don't want to use response from controller, you can create your own view class and use response there.
Create a class extending AbstractView, override its renderMergedOutputModel to write a binary stream from model to the response stream, and declare it as the "image" view using BeanNameViewResolver.
-
Nov 29th, 2009, 08:47 PM
#3
need controller to return image as MODEL instead of binary.
Thanks for the suggestion but I was wondering if there is a better way to utilize controller itself instead of writing another Abstractview program. The reason being I want image as an attribute of MODEL directly being rendered via the controller to view something similar to strings submitted as request attributes and returned by controller .
thanks
-
Nov 30th, 2009, 09:42 AM
#4
Then you can use OutputStream from HttpServletResponse (if your controller implements Controller), or pass OutputStream itself as a parameter (if you use annotation-based controllers).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules