0

Hello fellow programmers,

I am writing a Spring MVC application for students to do an online assessment with multiple choice questions. An admin should be able to create assessments, so I created this object structure:

@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
}

Now, I am using a JSP form on the admin page:

Controller

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

    return "admin-assessments-create";
}

JSP form

<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>

When I go to this page I receive the following error:

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?

I checked all the getters and setters but those are perfectly fine.

Question:

how do I avoid the NotReadablePropertyException and thus bind the nested Answer List to my form?

1 Answer 1

4

Use

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

and it will be working.

Why? For boolean fields you have to adapt the get/set paradigm to "is"XYZ(). For the EL expression, you have to drop the "is" in front of the method that accesses the current value of the field, pretty much the same way, you would do with "get" / "set".

Sign up to request clarification or add additional context in comments.

1 Comment

So the problem wasn't at all about the nestedness.. well that's awkward. Thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.