问题描述
- java.lang.ClassCastException
- struts2 action
private List<GanttTask> ganttTaskList = null; ganttTaskList = ganttTaskDao.gtList(task_id); for(int i = 0;i<ganttTaskList.size();i++){ cname = ganttTaskList.get(i).getOther_field(); }
这句话有什么错?为什么总是报错
java.lang.ClassCastException: org.apache.commons.collections.map.ListOrderedMap cannot be cast to com.smic.mfg.bean.GanttTask at com.smic.mfg.action.TaskListAction.query(TaskListAction.java:102) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662)
解决方案
rg.apache.commons.collections.map.ListOrderedMap cannot be cast to com.smic.mfg.bean.GanttTask List<GanttTask> ganttTaskList = ganttTaskDao.gtList(task_id); //看看这个方法返回的list和你定义的是否一致。
解决方案二:
org.apache.commons.collections.map.ListOrderedMap cannot be cast to com.smic.mfg.bean.GanttTaskListListOrderedMap不能转换为GanttTask ganttTaskDao.gtList() 这个方法的返回类型应该是定义成了ListListOrderedMap看错误应该是 ganttTaskDao.gtList() 这个方法定义的返回类型是错了,可能是import的时候搞错了。
解决方案三:
# 类转换异常啊,这个错你都能犯?
解决方案四:
dao
List<GanttTask> gtList(String task_id) throws Exception;
impl
@Override public List<GanttTask> gtList(String task_id) throws Exception { String sql = ""select b.task_idnamepercent_doneto_char(a.start_date'yyyy-mm-dd')start_dateto_char(a.end_date'yyyy-mm-dd')end_date +other_fielddurationpid""index"" from task aJMP_TB_PROJECT b where a.pid=to_number(SUBSTR(b.TASK_ID66)) and b.task_id=? ""; return jt.queryForList(sqlnew Object[]{task_id}); }
bean
package com.smic.mfg.bean;import java.io.UnsupportedEncodingException;import java.util.Date;public class GanttTask { private Integer id; private Date start_date; private Date end_date; private Integer percent_done; private String name; private Integer priority; private String baseline_start_date; private String baseline_end_date; private Integer parent_id; private Double duration; private String duration_unit; private String other_field; private Integer pid; private Integer index; private String task_id; public String getTask_id() { return task_id; } public void setTask_id(String task_id) { this.task_id = task_id; } public Date getStart_date() { return start_date; } public void setStart_date(Date start_date) { this.start_date = start_date; } public Date getEnd_date() { return end_date; } public void setEnd_date(Date end_date) { this.end_date = end_date; } public String getBaseline_start_date() { return baseline_start_date; } public void setBaseline_start_date(String baseline_start_date) { this.baseline_start_date = baseline_start_date; } public String getBaseline_end_date() { return baseline_end_date; } public void setBaseline_end_date(String baseline_end_date) { this.baseline_end_date = baseline_end_date; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPercent_done() { return percent_done; } public void setPercent_done(Integer percent_done) { this.percent_done = percent_done; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPriority() { return priority; } public void setPriority(Integer priority) { this.priority = priority; } public Integer getParent_id() { return parent_id; } public void setParent_id(Integer parent_id) { this.parent_id = parent_id; } public Double getDuration() { return duration; } public void setDuration(Double duration) { this.duration = duration; } public String getDuration_unit() { return duration_unit; } public void setDuration_unit(String duration_unit) { this.duration_unit = duration_unit; } public String getOther_field() throws UnsupportedEncodingException { return other_field; } public void setOther_field(String other_field) { this.other_field = other_field; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public Integer getIndex() { return index; } public void setIndex(Integer index) { this.index = index; }}
问题何在?
时间: 2024-12-05 18:06:33