Repopulate ArrayList from JSP with Struts 2

后端 未结 3 1874
抹茶落季
抹茶落季 2020-12-04 03:24

This is the form I am using to repopulate the ArrayList

相关标签:
3条回答
  • 2020-12-04 03:54

    Unexpected Exception caught setting 'quizItem.question' on 'class quiz.actions.QuizTemplateAction: Error setting expression 'quizItem.question' with value '[Ljava.lang.String;@1b3409f'

    You are trying to send all the questions (attribute) descriptions into the first Question (object) as a List<String>, because you are not specifying the index (as you correctly do with <s:property/> in your other questions... ?!).

    Change this

    <s:textfield name = "quizItem.question"/> 
    

    To this

    <s:textfield name = "quizItem[%{#key.index}].question"/>
    

    To send a single String to each correspondent Question object, instead of a List<String> to the first Question object.

    0 讨论(0)
  • 2020-12-04 04:10
        Where myQuestions is a List of Question Objects. 
        upon submission this gives me an error
    

    Since it is a list of Questions Objects you are trying to populate a Question Object with a String. Please check if you have the converter defined to covert String into Question and also specified in the xwork-conversion.properties file

    System.out.println(myQuestions); prints an empty list.
    

    instead of doing this

    private List<Question> myQuestions = new ArrayList<Question>();
    

    do this

    private List<Question> myQuestions;
    

    When you are submiting the form, a new object of your Action class is created and your instance variable "myQuestions" gets reinitialized with each submission.

    Hope this helps :)

    0 讨论(0)
  • 2020-12-04 04:15

    When you submit the form Struts2 uses parameters named the same as field names. These parameters are populated to the action by the params interceptor which sets the values to the valueStack. Since the action is on top of the stack its properties will be set.

    In your action you have an List<Question> but passing List<String>.

    Built in Type Conversion Support:

    collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created

    To fix the problem use indexed property names like this

    <s:textfield name = "myQuestions[%{#key.index}].question"/> 
    

    0 讨论(0)
提交回复
热议问题