迭代列表后无法在jsp中获取值

时间:2013-01-10 14:17:34

标签: java jsp java-ee servlets struts1

我正在尝试将我的dao类列表添加到我的jsp页面中。

我的列表从我的DAO类返回正常,但在将返回列表迭代到我的jsp页面时,我无法获取表列的值

请帮我解决这个问题。

控制台出错:

org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: java.lang.Integer cannot be cast to         
com.ebhasin.bstalentscareers.beans.Bsmostviewjp
    at org.apache.jsp.jsps.BSHomePage_jsp._jspService(BSHomePage_jsp.java:594)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    ------------
    ----------

DaoCLass.java

public List getpopularJobProvider()
  {
  List list = null;
  try
   {
     session=HibernateUtil.getSessionFactory().openSession();
     transaction=session.beginTransaction();
     ProjectionList pl=Projections.projectionList();
     pl.add(Projections.distinct(Projections.property("jobProviderId")));
     list=session.createCriteria(Bsmostviewjp.class).addOrder(Order.desc("counter")).setProjection(pl).setMaxResults(4).list();
     System.out.println("List  of the most popular job provider in dao-"+list.size());//this is fine
     transaction.commit();
   }
    catch (Exception e)
    {
         if(transaction!=null && transaction.isActive())
                {
                    try
                    {
                         transaction.rollback();
                    }
                    catch (Exception e1)
                    {
                         System.out.println("Exception in getpopularJobProvider Rollback  :" + e1);
                    }
                }
        System.out.println("Exception in getpopularJobProvider :"+e);
    }
    return list; 
}

jspPage.jsp

 <tr> 
     <%
     List mostpopjplist=jposting.getpopularJobProvider();
       Iterator mostit=mostpopjplist.iterator();
         while(mostit.hasNext())
         { 
          Bsmostviewjp bsmostviewjp=(Bsmostviewjp) mostit.next();//Problem here  
         Integer jpId=bsmostviewjp.getJobProviderId(); 
     %>
  <td>
    <%=jpId%> 
   </td>
       <%}%>
</tr>

Bsmostviewjp.java(Bean / pojo CLass)

import java.util.Date; 
public class Bsmostviewjp  implements java.io.Serializable {


     private Integer mvjpId;
     private int jobSeekerId;
     private int jobId;
     private int jobProviderId;
     private int counter;
     private Date appliedOn;

    public Bsmostviewjp() {
    }

    public Bsmostviewjp(int jobSeekerId, int jobId, int jobProviderId, int counter, Date appliedOn) {
       this.jobSeekerId = jobSeekerId;
       this.jobId = jobId;
       this.jobProviderId = jobProviderId;
       this.counter = counter;
       this.appliedOn = appliedOn;
    }

    public Integer getMvjpId() {
        return this.mvjpId;
    }

    public void setMvjpId(Integer mvjpId) {
        this.mvjpId = mvjpId;
    }
    public int getJobSeekerId() {
        return this.jobSeekerId;
    }

    public void setJobSeekerId(int jobSeekerId) {
        this.jobSeekerId = jobSeekerId;
    }
    public int getJobId() {
        return this.jobId;
    }

    public void setJobId(int jobId) {
        this.jobId = jobId;
    }
    public int getJobProviderId() {
        return this.jobProviderId;
    }

    public void setJobProviderId(int jobProviderId) {
        this.jobProviderId = jobProviderId;
    }
    public int getCounter() {
        return this.counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
    public Date getAppliedOn() {
        return this.appliedOn;
    }

    public void setAppliedOn(Date appliedOn) {
        this.appliedOn = appliedOn;
    } 
}

3 个答案:

答案 0 :(得分:0)

首先,您不应使用List和Iterator而不进行参数化。 我的意思是你必须指出你列出的元素类型。因此,您应该写列表&lt;整数&gt; 迭代器&lt;整数&gt; 而不是列表迭代器 >列表与LT; Bsmostviewjp&GT; 即可。这将允许编译器在出现问题时更容易告诉您。见this very good explanation of why。这为您提供了类型安全性,这非常有用。

完成此操作后,我猜您会很快发现错误。

其次,你不应该在jsp中使用scriptlet,而是使用taglibs。请参阅this

答案 1 :(得分:0)

而不是

  

列表list = null;

使用

  

列表与LT; Bsmostviewjp&gt; list = null;

答案 2 :(得分:0)

Integer bsmostviewjp=(Integer) mostit.next();是Stefan Be建议的解决方案