繁体   English   中英

如何将嵌套的List对象绑定到JSP表单?

[英]How to bind nested List object to JSP form?

各位程序员大家好,

我正在编写一个Spring MVC应用程序,供学生进行多项选择题的在线评估。 管理员应该能够创建评估,所以我创建了以下对象结构:

@Entity
@Table(name = "assessment")
public class Assessment {
    private List<Question> questions;
    // getter and setter
}


@Entity
@Table(name = "question")
public class Question {
    private String questionText;
    private List<Answer> answers;
    // getters and setters
}


@Entity
@Table(name = "answer")
public class Answer {
    private String answerText;
    private boolean isCorrect;
    // getters and setters
}

现在,我在管理页面上使用了JSP表单:

调节器

@RequestMapping(value = "/add/assessment", method = RequestMethod.GET)
public String addAssessments(Model model) {
    model.addAttribute("assessmentModel", new Assessment());

    return "admin-assessments-create";
}

JSP表格

<form:form method="POST" modelAttribute="assessmentModel">
    <form:input path="questions[0].questionText" type="text"/> <!-- this is working-->

    <form:radiobutton path="questions[0].answers[0].isCorrect"/> <!-- not working-->
    <form:input path="questions[0].answers[0].answerText"/>

    <button class="btn" type="submit">Submit</button>
</form:form>

当我转到此页面时,收到以下错误:

org.springframework.beans.NotReadablePropertyException:
    Invalid property 'questions[0].answers[0].isCorrect' of bean class [com.johndoe.model.Question]:
    Bean property 'questions[0].answers[0].isCorrect' is not readable or has an invalid getter method:
    Does the return type of the getter match the parameter type of the setter?

我检查了所有的吸气剂和吸气剂,但都很好。

题:

如何避免NotReadablePropertyException从而将嵌套的答案列表绑定到表单?

采用

<form:radiobutton path="questions[0].answers[0].correct"/>

它将起作用。

为什么? 对于boolean字段,您必须将get / set范例修改为"is"XYZ() 对于EL表达式,您必须在访问字段的当前值的方法之前放置“ is ”,几乎与“ get” /“ set”相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM