【问题标题】:unable to get values inside jsp after iterating a list迭代列表后无法在jsp中获取值
【发布时间】:2013-01-10 14:17:33
【问题描述】:

我正在尝试将列表从我的 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;
    } 
}

【问题讨论】:

  • 请添加你的jsp的全部代码
  • 看起来您的列表包含整数值。您应该使用调试器在打印出大小的位置查看列表值。
  • @micha 正确的列表返回整数值。那么如何解决这个问题
  • 整数 bsmostviewjp=(整数) mostit.next();
  • @StefanBe 谢谢!您的评论Integer bsmostviewjp=(Integer) mostit.next(); 解决了我的问题

标签: java jsp jakarta-ee servlets struts1


【解决方案1】:

首先,你不应该在没有参数化的情况下使用 List 和 Iterator。 我的意思是您必须指出您列出的元素类型。因此,您应该编写 ListIterator,而不是 ListIterator >列表。这将允许编译器在出现问题时更容易地告诉您。见this very good explanation of why。这为您提供了类型安全性,这非常有用。

做完之后,我想你会很快发现错误的。

其次,你不应该在jsp中使用scriptlet,而应该使用taglibs。见this

【讨论】:

    【解决方案2】:

    代替

    列表列表 = null;

    使用

    列表 list = null;

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2014-07-26
        • 2018-12-19
        • 1970-01-01
        • 1970-01-01
        • 2021-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-11
        相关资源
        最近更新 更多