【问题标题】:Repopulate ArrayList from JSP with Struts 2使用 Struts 2 从 JSP 重新填充 ArrayList
【发布时间】:2013-03-17 01:24:48
【问题描述】:

这是我用来重新填充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 是问题对象列表。提交后,这给了我一个错误

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() 从另一个填充了

【问题讨论】:

    标签: java list jsp struts2 ognl


    【解决方案1】:
        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<Question> myQuestions = new ArrayList<Question>();
    

    这样做

    private List<Question> myQuestions;
    

    当您提交表单时,会创建 Action 类的新对象,并且每次提交都会重新初始化您的实例变量“myQuestions”。

    希望这会有所帮助:)

    【讨论】:

    • 顺便问一下问题是一个抽象类
    • BUt 当我执行此私有 List myQuestions = new ArrayList();它给了我一个空值
    • 如果你需要来自 JSP 的填充值,你需要简单地把 List myQuestions;现在请通过在其中打印一个虚拟值来检查 JSP 流是否在迭代器标记内。如果您使用的是 eclip,那么 ctrl+shift+R 将打开一个窗口,您可以在其中输入 xwork-conversion 并显示路径。如果你的 web 模块的资源文件夹中没有,那么你需要添加一个 xwork 文件。
    • 我添加了虚拟值,实际上有一个内容。虽然我没有任何 xwork-conversion
    • 添加它,然后为相应的问题对象子类制作转换器,并将其条目添加到xwork中。
    【解决方案2】:

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

    您正在尝试将所有问题(属性)描述作为List&lt;String&gt; 发送到第一个问题(对象)中,因为您没有指定索引(正如您在其他问题中正确使用&lt;s:property/&gt; 所做的那样...... . ?!).

    改变这个

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

    到这里

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

    向每个对应的Question 对象发送单个String,而不是向第一个Question 对象发送List&lt;String&gt;

    【讨论】:

      【解决方案3】:

      当您提交表单时,Struts2 使用名称与字段名称相同的参数。这些参数由params interceptor 填充到操作中,该操作将值设置为valueStack。由于该操作位于堆栈顶部,因此将设置其属性。

      在您的操作中,您有一个List&lt;Question&gt;,但传递了List&lt;String&gt;

      Built in Type Conversion Support:

      集合 - 如果不能确定对象类型,则假定为 String 并创建新的 ArrayList

      要解决此问题,请使用这样的索引属性名称

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

      【讨论】:

        猜你喜欢
        • 2014-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多