使用Struts 2从JSP重新填充ArrayList
这是我用来重新填充ArrayList
这是动作类
public class QuizTest extends ActionSupport{ public String execute(){ List q= myQuestions; System.out.println(myQuestions); return "success"; } public String populateQuestions(){ //more code here } public void setMyQuestions(List myQuestions) { this.myQuestions = myQuestions; } private List myQuestions = new ArrayList(); }
其中myQuestions
是问题对象列表。 提交后,这给了我一个错误
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()
填充了另一个
Unexpected Exception在’class quiz.actions.QuizTemplateAction上设置了’quizItem.question’:错误设置表达式’quizItem.question’,其值为'[Ljava.lang.String; @ 1b3409f’
您正尝试将所有问题(属性)描述作为List
发送到第一个Question(对象)中,因为您没有指定索引(正如您在其他问题中正确使用
。 ..?!)。
改变这个
对此
将单个String
发送到每个对应的Question
对象,而不是List
到第一个Question
对象。
Where myQuestions is a List of Question Objects. upon submission this gives me an error
由于它是一个问题对象列表,因此您尝试使用字符串填充问题对象。 请检查是否已定义转换器以将String转换为Question并在xwork-conversion.properties文件中指定
System.out.println(myQuestions); prints an empty list.
而不是这样做
private List myQuestions = new ArrayList ();
做这个
private List myQuestions;
提交表单时,将创建Action类的新对象,并在每次提交时重新初始化实例变量“myQuestions”。
希望这可以帮助 :)
当您提交表单时,Struts2使用与字段名称相同的参数。 这些参数由params
拦截器填充到动作,该拦截器将值设置为valueStack
。 由于操作位于堆栈顶部,因此将设置其属性。
在您的操作中,您有一个List
但是传递List
。
内置类型转换支持:
集合 – 如果不能确定对象类型,则假定它是一个
String
并创建一个新的ArrayList
要解决此问题,请使用这样的索引属性名称