问题描述
这是我用来重新填充 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>
这是行动类
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>();
}
其中 myQuestions
是问题对象列表。在提交时,这给了我一个错误
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'
和 System.out.println(myQuestions);
打印一个空列表。但在提交表单之前, myQuestions
已经通过此方法 populateQuestions()
从另一个填充
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
推荐答案
你是尝试将所有问题(属性)描述作为列表< String>
发送到第一个问题(对象),因为您没有指定索引(正如您正确处理的那样) < s:property />
在您的其他问题中......?!)。
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... ?!).
更改此
<s:textfield name = "quizItem.question"/>
到此
<s:textfield name = "quizItem[%{#key.index}].question"/>
向每位通讯员发送一张字符串
问题
对象,而不是列表< String>
到第一个问题
对象。
To send a single String
to each correspondent Question
object, instead of a List<String>
to the first Question
object.
这篇关于使用Struts 2从JSP重新填充ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!