Ordinary hut

人间一福地,胜似天仙宫
posts - 61, comments - 50, trackbacks - 0, articles - 1
今天研究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
    }
}

Feedback

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2009-04-23 17:55 by weid
good!问题解决,谢谢楼主!

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2009-07-06 00:09 by mega
写了 RoleAction 之后,怎么用?

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录[未登录]  回复  更多评论   

2009-09-07 08:24 by King
非常感谢,我终于找到了错误的原因

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2009-11-16 22:02 by ddq
感谢,一样的错误

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2010-01-18 09:21 by 11
谢谢楼主。

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2010-06-22 23:02 by Eric_jiang
我也遇到这个问题 谢谢指教

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2010-07-27 13:14 by South
大顶楼主!

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2010-10-20 17:25 by jwshuang21
还有可能是传入的参数在ACTION中没有定义全局变量。

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2010-10-26 23:57 by j
太感谢了一只在想这个问题

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2011-01-15 17:26 by ZJY
问题解决,太感谢楼主了

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2011-05-20 10:01 by 任超
我刚遇到这个问题,请问怎么去解决呀 新手 请教了!
加QQ 280096669

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2011-05-20 10:08 by 任超
有没有详细点的!

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录[未登录]  回复  更多评论   

2012-05-16 20:51 by 袁勇
楼主已经超神了!

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2012-10-31 16:13 by ww
神一般楼主啊

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2013-05-02 00:00 by woshishui
这是什么意思?

# 菜鸟求助!!!  回复  更多评论   

2013-11-12 18:36 by jackLin
楼主,这个问题我纠结一天了,希望您能指导指导啊!!!

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2014-01-05 13:54 by 公子
这样写的话,岂不是action中的每一个方法都会出现初始化一遍

加一个判断是妥妥的

# re: 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录  回复  更多评论   

2014-04-10 11:40 by 游客
问题搞定,神一般楼主啊,谢谢楼主。

只有注册用户登录后才能发表评论。


网站导航: