关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录

今天研究struts2的标签功能,在用checkboxlist的时候,
代码如下
< s:checkboxlist  list ="roles"  listKey ="id"  listValue ="name"  name ="selectRoleId" ></ s:checkboxlist >
出现了如下异常:

org.apache.jasper.JasperException: tag  ' checkboxlist ' , field  ' list ' , name  ' selectRoleId ' : The requested list key  ' roles '  could not be resolved as a collection / array / map / enumeration / iterator type. Example: people or people.{name}  -  [unknown location]
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:
541 )
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
435 )
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320 )
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:
266 )
    javax.servlet.http.HttpServlet.service(HttpServlet.java:
803 )
    org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:
154 )
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:
186 )
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:
361 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
265 )
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:
249 )
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:
68 )
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:
122 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:
195 )
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:
195 )
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:
148 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:
93 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:
235 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:
89 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:
128 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:
104 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:
267 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:
126 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:
138 )
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:
148 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:
164 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:
128 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:
176 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.interceptor.TokenInterceptor.handleValidToken(TokenInterceptor.java:
178 )
    org.apache.struts2.interceptor.TokenInterceptor.doIntercept(TokenInterceptor.java:
144 )
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87 )
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236 )
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:
52 )
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
468 )
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:
77 )
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:
76 )
出现这个错误的原因可能如下:

1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误

解决办法很简单,不能让list为空

2 刚进入该界面的时候list是有数据的,当点击提交等按钮的时候,数据被提交到后台,如果配置了验证框架或者在action中写了validate方法,校验没有通过,未走action,直接返回了input,又指定回了当前界面。
此时的checkboxlist中的list丢失了,导致了如上错误 (这个错误提示的不太友好,让人认为是类转换错误)

解决办法是把初始化list的工作放到prepare拦截器中,因为prepare是在validate拦截器之前执行,即实现prepareble接口:
public   class  RoleAction  extends  ActionSupport  implements  Preparable{
    @Override
    
public   void  prepare()  throws  Exception {
        
// 初始化list
    }
}

你可能感兴趣的:(关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录)