【问题标题】:DYNAMIC FORM: how to pass a data as multi-dimensional array (Struts 2)动态形式:如何将数据作为多维数组传递(Struts 2)
【发布时间】:2014-01-14 00:14:18
【问题描述】:

这就是他们所说的DYNAMIC FORMs吗?
HTML

<div id="myForm">
  <form method="post">
    <input type="text" name="question1" placeholder="Question 1"/>
    <input type="text" name="item1" placeholder="Item 1"/>

    <a href="#" onclick="addItem();">Add more items</a>
    <a href="#" onclick="addQuestion();">Add new question</a>
  </form>
</div>

<script> 
   function addQuestion() {
      // codes to add more input type=text like:
      $('#myForm').append('<input type="text" name="item2"/>');
   }
   function addItem() {
      // codes to add more input type=text like:
      $('#myForm').append('<input type="text" name="item2"/>');
   }
</script>

作为一名学生,想这些东西对我来说真的很复杂。
我的问题是,我不知道如何在表单上传递多个项目或数据。甚至在&lt;input&gt; 的name 属性上放置什么。此外,如果用户添加更多项目!我怎么能传递这些意外数量的数据。
当用户想要添加更多项目或选项时也是如此。
或者我应该实现这些逻辑:

<form>...</form>
<a href="#" onclick="addForm();">Add a question</a>
<script>//codes to add new form</script>

而我要做的就是知道如何传递多种形式。但是如果我这样做,Struts2(mvc 中的request.getParameter())会识别传递的数据吗?

请帮忙,这对于像我这样的学生来说太复杂了。

编辑: 要传递的数据就像一个多维数组。例如:

[question1, [item1, item2, item3]],
[question2, [item1, item2, item3, item4]]
[question3, [item1, item2]]

也就是说,在表单中,用户可以添加更多问题,并且在每个问题中用户可以添加更多项目。

主要功能是用户可以创建multiple-choice测验。所以在每个问题中,都会有任意数量的项目。 Where item = options or choices.

【问题讨论】:

  • 对于问题 nd 项目将在数组中具有相同的索引。

标签: jquery multidimensional-array struts2 dynamic-forms


【解决方案1】:

我已经为此做了一些事情,下面的示例代码.. in struts 2.x

addRow.jsp

<div id="myForm">
  <form method="post" action='submitForm.action'>
    <input type="text" name="questions" placeholder="Question 1"/>
    <input type="text" name="items" placeholder="Item 1"/>

    <a href="#" onclick="addItem();">Add more items</a>
    <a href="#" onclick="addQuestion();">Add new question</a>
 <input type="submit" value="submit" /> 
 </form>
</div>

<script> 
   function addQuestion() {
      // codes to add more input type=text like:
      $('#myForm').append('<input type="text" name="questions"/>');
   }
   function addItem() {
      // codes to add more input type=text like:
      $('#myForm').append('<input type="text" name="items"/>');
   }
</script>

AddRowAction.java

package action;    
import com.opensymphony.xwork2.ActionSupport;
    /**
     *
     * @author Prabhakar
     */
    public class AddRowAction extends ActionSupport{

    private String[] items;
    private String[] questions;

       public String[] getItems() {
            return items;
        }

        public void setItems(String[] items) {
            this.items = items;
        }

        public String[] getQuestion() {
            return question;
        }

        public void setQuestion(String[] question) {
            this.question = question;
        }


    public String getFormData()
    {

    for(String question : questions)

    System.out.println("The question in the index "+i+" is "+question)

    }
    for(String item : items)

    System.out.println("The Item in the index "+i+" is "+item)

    }
    return SUCCESS;
    }

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<!-- Author: Prabhakar -->
 <struts>

<package name="action.AddRowAction" extends="struts-default">

<action name="submitForm" class="action.AddRowAction"
        method="getFormData" >
    <result name="success">/addRow.jsp</result>
    </action>  
  </package>
</struts>

【讨论】:

  • 我如何知道特定项目属于哪个问题,属于哪个问题?
  • @FireStatic 用于问题 nd 项目将在数组中具有相同的索引。
  • 我的意思是,每个问题可以有很多项目。对不起,我还没有清除我发布的问题。但我要问的是用户可以添加一个问题,并且在每个问题中,用户都可以添加项目。所以它就像一个多维数组。 [questions, [item1, item2, item3]] , [question2, [item1,item2,item3,item4,item5]]
【解决方案2】:

您可以将请求变量映射到 struts2 中的 bean 列表。假设您已定义参数拦截器或正在使用 defaultStack。

1) 定义一个包含

的bean
String question;
List<String> items;

2) 将此 bean 公开为您的操作类中的列表

List<MyBean> questions;

3) 在你的 jsp 中像这样发送请求参数。

<input type="text" name="questions[0].question" placeholder="Question 1"/>
<input type="text" name="questions[0].items[0]" placeholder="Item 1"/>
<input type="text" name="questions[0].items[1]" placeholder="Item 2"/>
<script> 
   var qcounter = 1;
   var icounter = 2;
   function addQuestion() {
      // codes to add more input type=text like:
      $('#myForm').append('<input type="text" name="questions[' + qcounter + '].question" placeholder="Question ' + (++qcounter) + '"/>');

      icounter = 0;

   }
   function addItem() {
      // codes to add more input type=text like:
      $('#myForm').append('<input type="text" name="questions[' + (qcounter - 1) + '].items[' + icounter + ']" placeholder="Item ' + (++icounter) + '"/>');
   }
</script>

您必须考虑单击“添加问题”按钮时要执行的操作,在我的示例中,我假设您要重置新问题的项目数。

这是关于 struts2 中表格形式输入的类似问题。 POST an array of custom objects to a Struts 2 action 希望这能给你一个想法。

【讨论】:

    猜你喜欢
    • 2015-02-04
    • 2013-12-07
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多