问题描述
今天遇到一个非常郁闷的问题,先看下面代码 这个是JSP页面取值代码: <td width="102" height="30" align="left"><bean:write name="regediter" property="username"/></td> <td width="81" align="left"><bean:write name="regediter" property="password"/></td> <td width="76" align="left"><bean:write name="regediter" property="msn"/></td> <td width="68" align="left"><bean:write name="regediter" property="qq"/></td> 这个是Action 处理请求代码: public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { int current = 0 ; String id = request.getParameter("id"); List list ; if(id==null){ list = HibernateDbConnection.getInstance().findById(); }else{ list = HibernateDbConnection.getInstance().findById(Integer.valueOf(id)); } Regediter regediter = (Regediter)list; request.setAttribute("list", regediter); request.setAttribute("current", current); return mapping.findForward("success"); 这个是异常信息:avax.servlet.ServletException: java.lang.ClassCastException: java.util.ArrayList cannot be cast to customs.zheng.Web.Regediter 我标记的红色字体是错误的关键,寻求解决办法
解决方案
引用Regediter regediter = (Regediter)list;这样写肯定不对嘛!怎么能吧list 转换成一个 Regediter 对象呢?如果是一个对象的话(因为你是 findbyId) Regediter regediter = (Regediter)list.get(0); OK 了 试试
解决方案二:
Regediter regediter = (Regediter)list.get(0);