This is the form I am using to repopulate the ArrayList
<form method = "POST" action = "addItemsToTemplate">
<s:iterator value = "myQuestions" var = "quizItem" status="key">
<s:textfield name = "quizItem.question"/>
</s:iterator>
<input type = "submit" value = "submit"/>
</form>
This is the action class
public class QuizTest extends ActionSupport{
public String execute(){
List<Question> q= myQuestions;
System.out.println(myQuestions);
return "success";
}
public String populateQuestions(){
//more code here
}
public void setMyQuestions(List<Question> myQuestions) {
this.myQuestions = myQuestions;
}
private List<Question> myQuestions = new ArrayList<Question>();
}
Where myQuestions
is a List of Question Objects. upon submission this gives me an error
Unexpected Exception caught setting 'quizItem.question' on 'class quiz.actions.QuizTemplateAction: Error setting expression 'quizItem.question' with value '[Ljava.lang.String;@1b3409f'
and System.out.println(myQuestions);
prints an empty list. but the myQuestions
was already been populated from another by this method populateQuestions()
, before submitting the form
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
instead of doing this
do this
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 :)
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
To this
To send a single
String
to each correspondentQuestion
object, instead of aList<String>
to the firstQuestion
object.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 thevalueStack
. Since the action is on top of the stack its properties will be set.In your action you have an
List<Question>
but passingList<String>
.To fix the problem use indexed property names like this