【问题标题】:Spring MVC 3.X + JSTL binding data list to <form:input> fail because forEach's varStatusSpring MVC 3.X + JSTL 绑定数据列表到 <form:input> 失败,因为 forEach 的 varStatus
【发布时间】:2014-07-01 09:29:52
【问题描述】:

我从几天以来一直在与这个错误作斗争。我尝试了我在互联网上找到的所有方法,但似乎只有我有这个问题。

我有这样的 java 类:

Class ListItem{
  private int id;
}

Class ListContainer{
private List<ListItem> items;
}

还有一个这样的jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
...
<form:form method="post" action="myController" modelAttribute="listContainerModel">
         <c:forEach items="${listContainerModel.items}" var="currentItem" varStatus="i">
              <form:input type="text" path="items[${i.index}].id" name="id"/>
         </c:forEach>

   </form:form>

但我有这个例外

org.springframework.beans.InvalidPropertyException: Invalid property 'items[${i.index}]' of bean class [foo.bar.ListContainer]: 
Invalid index in property path 'items[${i.index}]'; nested exception is java.lang.NumberFormatException: For input string: "${i.index}"

我尝试了很多东西,例如

How to send list of Objects to View and back to Post method in controller 控制器中的后置方法

http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/

我想我在混合 jstl 标签 + el 表达式 + spring mvc 标签库时遇到了问题,但我不知道为什么。

【问题讨论】:

  • 参数名为varStatus,而不是Status
  • 是的,你是对的!我已经在我的帖子中解决了这个问题。顺便说一句,这不是问题。
  • 也请在varStatus下面的行中修正你的path,如果你想帮助你,你需要正确地发布你的问题
  • 没人知道这件事吗?

标签: spring model-view-controller foreach jstl


【解决方案1】:

我终于知道为什么了。

我的 web.xml 是由使用 servlet 2.3 版的模板生成的。在这个版本中,EL 表达式在 JSP 中默认被忽略。我只是改变了这个:

<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >

到那个

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"> 

然后就没事了!

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 2015-01-02
    • 2015-09-26
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多