2014-07-18 37 views
0

我试图从数据库中获取图像,我能够获取图像,现在我想要显示该图像到jsp页面。我正在使用hibernate和struts。我有动作类和一个hibernate持久类(POJO)。无法使用休眠和struts从数据库中显示图像jsp使用休眠和struts

Action类是SportsAction

package action; 

@Result(location="sports-success.jsp") 
public class SportsAction extends ActionSupport{ 

public List<SportsSetterAndGetter> l; 

public List<SportsSetterAndGetter> getL() { 
    return l; 
} 

public void setL(List<SportsSetterAndGetter> l) { 
    this.l = l; 
} 

private byte[] pic; 

public byte[] getPic() { 
    return pic; 
} 

public void setPic(byte[] pic) { 
    this.pic = pic; 
} 

@Action(value="sports") 
@Override 
public String execute() throws Exception { 
    Session session= new  AnnotationConfiguration().configure().buildSessionFactory().openSession(); 

    Transaction t= session.beginTransaction(); 

    l=session.createCriteria(SportsSetterAndGetter.class).list(); 

    Iterator itr1=l.iterator(); 

    SportsSetterAndGetter s=new SportsSetterAndGetter(); 

    while (itr1.hasNext()) { 
     s=(SportsSetterAndGetter)itr1.next(); 

     System.out.println("values are"+s.getSid()); 
     System.out.println(s.getName()); 
     System.out.println(s.getRate()); 
     System.out.println(s.getStatus()); 
     System.out.println(s.getStype()); 

     pic=s.getImage();   

    } 



    return "success"; 
} 
} 

在Jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="/struts-tags" prefix="s" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<s:iterator value="l"> 
<s:property value="sid" /> 
<h1><s:property value="name" /></h1> 
<h3><s:property value="rate" /></h3> 
<h3><s:property value="status" /></h3> 
<s:property value="stype" /> 
<img src="<s:property value="image" />" /> 
</s:iterator> 
</body> 
</html> 

我正在从数据库中这个值,但我无法获取图像。我应该写什么来获得形象。

+0

你是从数据库读取图像?如果是这样,为什么你要保留这一行“/>。图像是否可以在路径”image“的某个目录下物理使用?您应该使用byte []值来获取图像 –

+0

@ShaileshSaxena是图像从数据库中获取。如何使用byte []在jsp页面中获取图像,在图像是hibernate setter和getter中byte []的名称 – user3359130

+0

在您的SportsAction类中, ]? –

回答

3
 BASE64Encoder base64Encoder = new BASE64Encoder(); 
     StringBuilder imageString = new StringBuilder(); 
     imageString.append("data:image/png;base64,"); 
     imageString.append(base64Encoder.encode(bytes)); 
     String image = imageString.toString(); 

你可以设置图片的字符串为您模型,并将其发送给你的JSP,并设置为你的src像

<img src="<s:property value="image" />" /> 
+0

谢谢你,非常感谢你。 – user3359130

+0

你好欢迎我的朋友 – Pravin