如何使用jstl,servlet在jsp中显示输出

时间:2015-03-17 14:13:26

标签: java jsp maven servlets jstl

抱歉,我的英文。我开始研究技术和javaEE不清楚我的一些观点。我使用maven和模式DAO。在数据库中我添加图像,但我不知道如何使用jstl和servlet从jsp输出图像。请告诉我如何在jsp中获取图片

对象类发布

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();
}

PostDaoImpl

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:Property&#39; image&#39;不可读   输入app.web.landingpage.object.Posts at   javax.el.BeanELResolver $ BeanProperty.read(BeanELResolver.java:297)at at   javax.el.BeanELResolver $ BeanProperty.access $ 000(BeanELResolver.java:245)     在javax.el.BeanELResolver.getValue(BeanELResolver.java:85)   .............

1 个答案:

答案 0 :(得分:1)

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; }
}