SpringMVC,捕获像@ModelAttribute这样的对象列表

SpringMVC,捕获像@ModelAttribute这样的对象列表,spring,model-view-controller,spring-mvc,jstl,Spring,Model View Controller,Spring Mvc,Jstl,我有测试类和jsp页面,其中包含绑定到测试对象列表的输入。我想用编辑过的信息从jsp中获取测试列表。 我的测试班是: @Entity @Table(name = "[NewMVC].[dbo].[Tests]") public class Test { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; @Column

我有测试类和jsp页面,其中包含绑定到测试对象列表的输入。我想用编辑过的信息从jsp中获取测试列表。 我的测试班是:

@Entity
@Table(name = "[NewMVC].[dbo].[Tests]")
public class Test {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id; 

    @Column(name = "testQuestion")
    private String testQuestion;

    @Column(name = "optionOne")
    private String optionOne;

    @Column(name = "optionTwo")
    private String optionTwo;

    @Column(name = "optionThree")
    private String optionThree;

    @Column(name = "subjectType")
    private int subjectType;

    @Column(name = "correctOptionNumber")
    private int correctOptionNumber; 
        //gets, sets...
我有一个jsp,可以在其中编辑带有测试的列表:

   <form:form action="saveTestsEdits" method="POST" modelAttribute = "testWrapper">

    <c:forEach items="${testWrapper.testList}" var="test" varStatus="i">
    <h2> Test number ${i.index+1} </h2>
    <form:hidden value="${test.id}" path = "testList[${i.index}].id" />
    <table>
        <tr><td>Test Question:</td> <td><form:input path = "testList[${i.index}].testQuestion" value = "${test.testQuestion}"/> </td></tr>
        <tr><td>Option one: </td> <td><form:input path = "testList[${i.index}].optionOne" value= "${test.optionOne}"/> </td>
        <td>        
        <c:choose>
        <c:when test="${test.correctOptionNumber == 1}"> <form:radiobutton path = "testList[${i.index}].correctOptionNumber" value = "1" checked = "checked"/> </c:when>
        <c:otherwise><form:radiobutton path = "testList[${i.index}].correctOptionNumber" value = "1"/> </c:otherwise> 
        </c:choose> 
        </td>
        </tr>

        <tr><td>Option two: </td> <td><form:input path = "testList[${i.index}].optionTwo" value= "${test.optionTwo}"/> </td>
        <td>        
        <c:choose>
        <c:when test="${test.correctOptionNumber == 2}"> <form:radiobutton path = "testList[${i.index}].correctOptionNumber" value = "2" checked = "checked"/> </c:when>
        <c:otherwise><form:radiobutton path = "testList[${i.index}].correctOptionNumber" value = "2"/> </c:otherwise> 
        </c:choose> 
        </td>
        </tr>


        <tr><td>Option three: </td> <td><form:input path = "testList[${i.index}].optionThree" value= "${test.optionThree}"/> </td>
        <td>        
        <c:choose>
        <c:when test="${test.correctOptionNumber == 3}"> <form:radiobutton path = "testList[${i.index}].correctOptionNumber" value = "3" checked = "checked"/> </c:when>
        <c:otherwise><form:radiobutton path = "testList[${i.index}].correctOptionNumber" value = "3"/> </c:otherwise> 
        </c:choose> 
        </td>

        </tr>
        <tr><th>Subject type:</th></tr>
        <tr><td>Georaphy: </td> 
        <td>        
        <c:choose>
        <c:when test="${test.subjectType == 3}"> <form:radiobutton path = "testList[${i.index}].subjectType" value = "3" checked = "checked"/> </c:when>
        <c:otherwise><form:radiobutton path = "testList[${i.index}].subjectType" value = "3"/></c:otherwise> 
        </c:choose> 
        </td>
        </tr>

        <tr><td>Mathematics: </td> 
        <td>        
        <c:choose>
        <c:when test="${test.subjectType == 4}"><form:radiobutton path = "testList[${i.index}].subjectType" value = "4" checked = "checked"/> </c:when>
        <c:otherwise><form:radiobutton path = "testList[${i.index}].subjectType" value = "4"/></c:otherwise> 
        </c:choose> 
        </td>
        </tr>

        <tr><td>History: </td> 
        <td>        
        <c:choose>
        <c:when test="${test.subjectType == 5}"><form:radiobutton path = "testList[${i.index}].subjectType" value = "5" checked = "checked"/> </c:when>
        <c:otherwise><form:radiobutton path = "testList[${i.index}].subjectType" value = "5"/></c:otherwise> 
        </c:choose> 
        </td>
        </tr>

    </table>
    <br>        
    </c:forEach>
    <input type="submit" value="Submit" />
</form:form>
</body>
</html>
但这给了我一个例外:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mvc] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.models.TestWrapper]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.models.TestWrapper.<init>()] with root cause
java.lang.NoSuchMethodException: com.andrew.models.TestWrapper.<init>()
SEVERE:Servlet[appServlet]在路径为[/mvc]的上下文中的Servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.andrew.models.TestWrapper]:未找到默认构造函数;嵌套异常为java.lang.NoSuchMethodException:com.andrew.models.TestWrapper.()],并带有根本原因
java.lang.NoSuchMethodException:com.andrew.models.TestWrapper。()

多谢各位

异常
java.lang.NoSuchMethodException:xxx。(
在JVM无法实例化类xxx的对象时引发。在这种情况下,Spring无法实例化TestWrapper类

您有一个列表参数构造函数,但不是默认的。添加一个如下所示:

public class TestWrapper {

    public TestWrapper() {
    }
}

谢谢它有帮助:)我认为它需要一些额外的构造函数。但这个问题变得如此简单!设法使用你的问题信息和病毒帕特尔的答案让我的工作!被困了一天多了!
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mvc] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.models.TestWrapper]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.models.TestWrapper.<init>()] with root cause
java.lang.NoSuchMethodException: com.andrew.models.TestWrapper.<init>()
public class TestWrapper {

    public TestWrapper() {
    }
}