Spring webFlow使用复选框绑定对象列表

时间:2012-11-30 12:11:11

标签: spring type-conversion spring-webflow type-mismatch

我正在尝试为对象列表创建一个复选框列表。 在我的表单支持bean中,我有2个列表。一个列表,其中包含包含我所有组织的参考数据。另一个包含所选组织的列表。

我的页面加载正常,但是当我尝试提交它时,我得到以下异常:

 Caused by: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'typeMismatch on selectedOrganizations' for locale 'nl'.
    at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
    at org.apache.jsp.WEB_002dINF.views.flows.validation_jsp._jspService(validation_jsp.java:177)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:638)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:543)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:480)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
    at org.apache.jsp.tag.web.wizard_tag._jspx_meth_form_005fform_005f0(wizard_tag.java:198)
    at org.apache.jsp.tag.web.wizard_tag.doTag(wizard_tag.java:146)
    at org.apache.jsp.WEB_002dINF.views.management.roleinvitation.create.selectOrganizations_jsp._jspx_meth_iam_005fwizard_005f0(selectOrganizations_jsp.java:375)
    at org.apache.jsp.WEB_002dINF.views.management.roleinvitation.create.selectOrganizations_jsp._jspService(selectOrganizations_jsp.java:141)
    ... 126 more

在此处查找我的代码段:

JSP

<c:forEach items="${organizations}" var="${organization}">
  <form:checkbox cssClass="organizationCheckbox" path="selectedOrganizations" value="${organization}"/>
</c:forEach>

查看模型

public class CreateRoleInvitationViewModel implements Serializable {
  private List<OrganizationViewModel> selectedOrganizations;
  private List<OrganizationViewModel> organizations;
...
}

转换器

public class StringToOrganizationViewModel extends StringToObject {

  public StringToOrganizationViewModel() {
    super(OrganizationViewModel.class);
  }

  @Override
  protected Object toObject(String code, Class targetClass) throws Exception {
    OrganizationViewModel orgViewModel = new OrganizationViewModel();
    orgViewModel.setCode(code);
    return orgViewModel;
  }

  @Override
  protected String toString(Object object) throws Exception {
    return ((OrganizationViewModel)object).getCode();
  }

}

我已检查过转换器是否已使用且已使用。我的页面加载后,我传递toString(对象对象)方法。 当我尝试提交时,我没有传递toObject(字符串代码,Object objectClass),因为异常发生在它之前。

注意:当我只选择一个复选框时......一切正常。我可以看到我的支持bean包含一个带有一个条目的selectedOrganisations列表。当我选择多个复选框时,它会崩溃....

1 个答案:

答案 0 :(得分:1)

在您的jsp中,在forEach标记中您犯了一个小错误,它应该是var="organization"而不是var="${organization}"