使用Struts 2从JSP重新填充ArrayList [英] Repopulate ArrayList from JSP with Struts 2

查看:110
本文介绍了使用Struts 2从JSP重新填充ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来重新填充 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

推荐答案


在'class
quiz.actions.QuizTemplateAction上设置'quizItem.question'时出现意外异常:错误设置表达式
'quizItem.question'的值为'[Ljava.lang.String; @ 1b3409f'

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

你是尝试将所有问题(属性)描述作为列表< 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆