Hi all,
I'm working on a small portal project for handling questionnaires and now I'm facing a curious problem.
I have one abstract entity "Answer", two extending entities "TextAnswer" and "MarkAnswer". Finally there is the AnswerSheet entity that contains Answers.
Now I want to edit AnswerSheets using JSP and controller.Code:public abstract class Answer { long id; Question question;//answer also remembers its question AnswerSheet answerSheet; +getters/setter } public class TextAnswer extends Answer { String answer; + getters/setters } public class MarkAnswer extends Answer { int answerValue; + getters/setters } public class AnswerSheet { List<Answer> answers; }
In the controller I put the answersheet I want to edit to the "model".
Now in JSP:
Well, now when I try to submit the form, spring complains about not being able to instantiate the Answer class (which is quite obvious, stacktrace is at the end of the post). Now I ask whether it is possible to tell spring to instantiate the TextQuestion (or MarkQuestion) class in jsp for editing.Code:<form:form action="${saveSheetAction}" method="post" modelAttribute="answerSheet"> <form:hidden path="id"/> //the id of answerSheet <c:forEach var = "answer" items = "${answerSheet.answers}" varStatus="answersLoop"> <form:hidden path="answers[${answersLoop.index}].id"/> <br/> <c:if test="${answer.question.type == 'TEXT'}"> <form:input path="answers[${answersLoop.index}].text"/> </c:if> <c:if test="${answer.question.type == 'MARK'}"> <form:input path="answer.answerValue"/> </c:if> </c:forEach> </form:form>
One solution would be using some kind of more intelligent DTO in the form and then later (in the controller) instantiate the correct class. But I was expecting something simpler
I'll be happy for any constructive response.
Thank you
Stacktrace:
Code:Invalid property 'answers[0]' of bean class [eu.ibacz.k3.gadgets.questionnaire.entities.AnswerSheet]: Invalid index in property path 'answers[0]'; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'answers' of bean class [eu.ibacz.k3.gadgets.questionnaire.entities.AnswerSheet]: Could not instantiate property type [eu.ibacz.k3.gadgets.questionnaire.entities.Answer] to auto-grow nested property path: java.lang.InstantiationException



Reply With Quote