Reinstall ArrayList from JSP with Struts 2

This is the form I use to 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 an 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. after submission it 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 myQuestions has already been populated from another by this populateQuestions() method before submitting the form

+1
source share
3 answers

An unexpected exception caught the parameter 'quizItem.question' on 'class quiz.actions.QuizTemplateAction: error definition' quizItem.question 'with value' [Ljava.lang.String; @ 1b3409f '

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

Change it

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

For this

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

To send one String each Question corresponding object instead of List<String> in the first Question object.

+2
source
  Where myQuestions is a List of Question Objects. upon submission this gives me an error 

Since this is a list of question objects that you are trying to fill a question object with a string. Check if you have the converter defined for the hidden line in the question, as well as the one specified in the xwork-conversion.properties file

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

instead of this

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

do it

 private List<Question> myQuestions; 

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

Hope this helps :)

+2
source

When submitting a form, Struts2 uses parameters with a name similar to field names. These parameters are populated with the action using params interceptor , which sets values ​​to valueStack . Since the action is on top of the stack, its properties will be set.

In your action, you have a List<Question> , but a List<String> transfer.

Native type conversion support:

collections - if the type of the object cannot be determined, it is assumed that it is 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
source

All Articles