Struts2列表无法解析

时间:2011-12-22 21:10:20

标签: struts2 struts

请帮帮我。我面临这个错误3天。我在网上搜索了所有的例子,但我无法弄明白。

这些是我的文件:

  

struts.xml中

...
<action name="menu"  class="com.struts.actions.MenuAction" method="asdf">
<result name="success" type="tiles">struts2tiles.home</result>
</action>
...
  

menu2.jsp

....
<body>
<s:form action="menu">
    <s:checkboxlist label="What's your favor color" list="colors" name="colors" />

<s:submit value="submit" name="submit" />

</s:form>
</body>
  

MenuAction

public class MenuAction extends ActionSupport {
    private ArrayList<String> colors;
    private String yourColor;

    public ArrayList<String> getColors() {
        return colors;
    }

    public void setColors(ArrayList<String> colors) {
        this.colors = colors;
    }

    public String getYourColor() {
        return yourColor;
    }

    public void setYourColor(String yourColor) {
        this.yourColor = yourColor;
    }

    public String asdf() {
        colors = new ArrayList<String>();
        colors.add("red");
        colors.add("yellow");
        colors.add("blue");
        colors.add("green");

        return SUCCESS;
    }

    @Override
    public String execute() {
        System.out.println("execute menu action");
        colors = new ArrayList<String>();
        colors.add("red");
        colors.add("yellow");
        colors.add("blue");
        colors.add("green");

        return SUCCESS;
    }

}

我做过的任何事情仍然存在同样的错误:

Dec 22, 2011 10:56:44 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
tag 'checkboxlist', field 'list', name 'colors': The requested list key 'colors' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
    at org.apache.struts2.components.Component.fieldError(Component.java:237)
    at org.apache.struts2.components.Component.findValue(Component.java:358)
    at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
    at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:856)
    at org.apache.struts2.components.UIBean.end(UIBean.java:510)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.jsp.common.menu2_jsp._jspx_meth_s_005fcheckboxlist_005f0(menu2_jsp.java:166)
    at org.apache.jsp.jsp.common.menu2_jsp._jspx_meth_s_005fform_005f0(menu2_jsp.java:122)
    at org.apache.jsp.jsp.common.menu2_jsp._jspService(menu2_jsp.java:84)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:927)
    at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
    at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
    at org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:80)
    at org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:73)
    at org.apache.tiles.context.TilesRequestContextWrapper.dispatch(TilesRequestContextWrapper.java:72)
    at org.apache.struts2.tiles.StrutsTilesRequestContext.dispatch(StrutsTilesRequestContext.java:88)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:283)
    at org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:140)
    at org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:117)
    at org.apache.tiles.jsp.taglib.RenderTagSupport.execute(RenderTagSupport.java:154)
    at org.apache.tiles.jsp.taglib.RoleSecurityTagSupport.doEndTag(RoleSecurityTagSupport.java:75)
    at org.apache.tiles.jsp.taglib.ContainerTagSupport.doEndTag(ContainerTagSupport.java:80)
    at org.apache.jsp.jsp.common.mainLayout_jsp._jspx_meth_tiles_005finsertAttribute_005f2(mainLayout_jsp.java:181)
    at org.apache.jsp.jsp.common.mainLayout_jsp._jspService(mainLayout_jsp.java:94)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
    at org.apache.tiles.servlet.context.ServletTilesRequestContext.include(ServletTilesRequestContext.java:217)
    at org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:177)
    at org.apache.tiles.context.TilesRequestContextWrapper.dispatch(TilesRequestContextWrapper.java:72)
    at org.apache.struts2.tiles.StrutsTilesRequestContext.dispatch(StrutsTilesRequestContext.java:88)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:606)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:246)
    at org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
    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.components.ActionComponent.executeAction(ActionComponent.java:291)
    at org.apache.struts2.components.ActionComponent.end(ActionComponent.java:173)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.index_jsp._jspx_meth_s_005faction_005f0(index_jsp.java:108)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:76)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

有人有想法吗?

7 个答案:

答案 0 :(得分:6)

请求的列表键&#39;颜色&#39;无法解析为集合/数组/映射/枚举/迭代器类型

当您尝试访问尚未创建的列表/集合时,会发生此错误。

可能是你直接调用了行动,即loacalhost:8080 / youraction.action。

请确保在实例化操作类之后访问列表,即调用相应的操作。

如果要在调用action之前直接访问它,请将其设置为静态并在jsp中访问它。

答案 1 :(得分:3)

简单来说,您直接访问页面即menu2.jsp而不处理任何操作,因此不会初始化列表。 在去往menu2.jsp的路上进行操作并初始化列表 错误说的是什么

The requested list key 'colors' could not be resolved as a collection/array/map/enumeration/iterator type

希望这有帮助

答案 2 :(得分:2)

有三种方法可以做到。

  1. 如果我们在类级别初始化所有集合对象,则不会发生此问题。根据我的理解,当此时触发特定操作时,JSP页面不知道字段的类型。在这种情况下,当你写 “private ArrayList<String> colors;”代替此,将其设为

    private ArrayList colors = new ArrayList();

  2. 使用像Tomasz Gutkowski这样的准备之前说过,也有效。

  3. 通过使用拦截器标签,我们可以这样说(包含在动作标签中)

    interceptor-ref name =“params”
    interceptor-ref name =“prepare”
    interceptor-ref name =“basicStack”

答案 3 :(得分:1)

您需要添加工具Preparable和方法prepare(),并在此方法中准备颜色列表。

应该是这样的:

public class MenuAction extends ActionSupport implements Preparable{
    private ArrayList<String> colors;
    private String yourColor;

    public ArrayList<String> getColors() {
        return colors;
    }

    public void setColors(ArrayList<String> colors) {
        this.colors = colors;
    }

    public String getYourColor() {
        return yourColor;
    }

    public void setYourColor(String yourColor) {
        this.yourColor = yourColor;
    }

    public String asdf() {
        colors = new ArrayList<String>();
        colors.add("red");
        colors.add("yellow");
        colors.add("blue");
        colors.add("green");

        return SUCCESS;
    }

    @Override
    public String execute() {
        System.out.println("execute menu action");


        return SUCCESS;
    }
    public void prepare() throws Exception {
        colors = new ArrayList<String>();
        colors.add("red");
        colors.add("yellow");
        colors.add("blue");
        colors.add("green");

    }
}

答案 4 :(得分:1)

我有同样的问题;我删除了struts xml验证,它工作正常

答案 5 :(得分:0)

在从jsp文件中的操作类访问任何字段之前,您需要确保创建操作类对象并正确初始化正在访问的字段。

在struts.xml中,对于操作'menu',您已配置为调用方法'asdf'。首先要注意的是struts框架不会调用execute方法来执行你的动作'menu',而是调用方法'asdf'。

在menu2.jsp中的表单中,在标记中,您尝试访问操作类中存在的变量命名颜色。因此,当调用menu2.jsp时,不会调用相应的操作,因此不会初始化字段颜色。

那么如何在menu2.jsp之前调用MenuAction类?这是实现这一目标的一次尝试:

的struts.xml

<!-- Define one extra action in struts.xml which redirects-->
<action name="invokeMenuActionFirst" class="com.struts.actions.MenuAction" method="input">
    <result name="input">/menu2.jsp</result>
</action>

无论您何时直接调用menu2.jsp,都需要调用“invokeMenuActionFirst”操作。例如,考虑如下示例other.jsp。

other.jsp

<a href="menu2.jsp">Link </a>
<%-- Replace links similar to above created using plain html markup with following struts2 markup. --%>
<a href="<s:url action='invokeMenuActionFirst'/>">Link </a>

这里的输入方法由ActionSupport类提供,其默认实现返回String“input”。 您还应该将颜色的初始化代码移动到MenuAction默认构造函数。

我从https://struts.apache.org/docs/message-resource-files.html得到了这个想法。

答案 6 :(得分:0)

必须为操作方法配置

struts.xml文件,该方法将如下所述填充列表/地图/集合。
<action name="selectAction" class="<full class name>" method="display"> <result name="none">pages/<file name>.jsp</result> </action

在浏览器中输入如下所述的URL:
http://hostname:port/context/selectAction.action
现在将填充集合,并且struts select将显示集合的内容