对不起,我的英语。我开始研究该技术,而javaEE对我来说有些不清楚。我使用Maven和模式DAO。在数据库中,我添加了图像,但是我不知道如何使用jstl和servlet从jsp输出图像。请告诉我如何在jsp中获取图片
对象类Post:
public class Posts {
//code
@Lob
@Column(name="IMAGE", nullable=false)
private byte[] image;
}
//code
public void setImage(byte[] image) { this.image = image; }
public byte[] getImge() { return image; }
}
PostDao界面:
public interface PostDao {
//code
public Collection getAllPost();
}
后道
public class PostDaoImpl implements PostDao{
//code
public Collection getAllPost() { //this method return list object
Session session = null;
List posts = new ArrayList<Posts>();
try{
session = HibernateUtil.getSessionFactory().openSession();
posts = session.createCriteria(Posts.class).list();
} catch(Exception e) { outputError("getAllPost", e);
} finally { closeSession(session); }
return posts;
}
//code
}
在servlet indexuser中
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
//get session
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
try{
//get collection allpost object
Collection allpost = Factory.getInstance().getPostDAO().getAllPost();
request.setAttribute("allpost", allpost);
request.getRequestDispatcher("/index.jsp").forward(request, response);
} catch(Exception e) {
System.out.println(e);
} finally{
if(session!=null && session.isOpen())
session.close();
}
}
和index.jsp使用JSTL
<c:forEach var="allpost" items="${allpost}">
<img src="${allpost.image}" alt="...">
</c:forEach>
错误:
javax.el.PropertyNotFoundException:属性“图像”在以下位置不可读
键入app.web.landingpage.object.Posts at
javax.el.BeanELResolver $ BeanProperty.read(BeanELResolver.java:297)在
javax.el.BeanELResolver $ BeanProperty.access $ 000(BeanELResolver.java:245)
在javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
.............
最佳答案
您的Posts#getImge
中有一个错字,应该是Posts#getImage
。当您使用EL访问类的属性时,它将调用该属性的get方法。
因此,如下更改您的Post
public class Posts {
@Lob
@Column(name="IMAGE", nullable=false)
private byte[] image;
//code
public void setImage(byte[] image) { this.image = image; }
public byte[] getImage() { return image; }
}