Results 1 to 1 of 1

Thread: Edit entity on the web tier

  1. #1
    Join Date
    May 2010
    Posts
    1

    Question Edit entity on the web tier

    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.

    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; 
    }
    Now I want to edit AnswerSheets using JSP and controller.
    In the controller I put the answersheet I want to edit to the "model".
    Now in JSP:

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

    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
    Last edited by integral; Jun 3rd, 2010 at 05:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •