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

查看:40
本文介绍了使用 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"上设置quizItem.question"时捕获到意外异常quiz.actions.QuizTemplateAction:错误设置表达式'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'

您正在尝试将所有问题(属性)描述作为 List 发送到第一个问题(对象)中,因为您没有指定索引(正如您对 <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"/>

向每个对应的 Question 对象发送单个 String,而不是将 List 发送到第一个 Question 对象.

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天全站免登陆