【问题标题】:How to send array to struts action class via ajax [duplicate]如何通过ajax将数组发送到struts动作类[重复]
【发布时间】:2016-09-19 11:12:05
【问题描述】:

我无法通过 ajax 调用将由 struts 迭代器迭代的动态数据作为数组发送到 struts 动作类作为单独的数组,以及如何在 struts 动作类中接收

这是我的示例代码:

struts 迭代

<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example3">
    <thead>
    <tr>
    <th><a href="#" id="selectall">Select all</a></th>
    <th>Student Name</th>
    <th>Phone</th>
    <th>Email</th>
    <th>ReferenceId</th>
    </tr>
    </thead>
    <tbody>
    <s:iterator value="adminSms">
    <tr>
    <td><s:checkbox name="refIDS" cssClass="case" fieldValue="%{ref}" /></td>
    <td><s:property value="studentname" /></td>
    <td id="phone"><s:property value="phone" /></td>
    <td id="email"><s:property value="email" /></td>
    <td><s:property value="ref" /></td>
    </tr>
    </s:iterator>
    </tbody>
    </table>

Ajax 调用

$(document).ready(function() {
            $('Send').click(function() {
                var Data = {
                    phone : $('#phone').val(),
                    email : $('#email').val()
                }
                $.ajax({
                    type : 'POST',
                    url : 'myURL',
                    data : JSON.stringify(Data)
                });
            });
        });

动作类

public class MyActionClass extends ActionSupport {

    private String [] phoneNumbers;
    private String [] emails;

    public String[] getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(String[] phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

    public String[] getEmails() {
        return emails;
    }

    public void setEmails(String[] emails) {
        this.emails = emails;
    }

    @Override
    public String execute() throws Exception {
        for (int i = 0; i < emails.length; i++) {
            System.out.println(emails[i]);
        }
        for (int i = 0; i < phoneNumbers.length; i++) {
            System.out.println(phoneNumbers[i]);
        }
        return SUCCESS;
    }
}

【问题讨论】:

  • 您可以通过 Ajax 发送。
  • 只发送 id 并检索对象。
  • Send 中的$('Send') 是什么?你想达到什么目的?为什么不只发送 id?
  • 当我点击提交时,我需要将电话和电子邮件数据发送到 struts 动作类
  • 你需要ajax吗?为什么不简单的形式或与参数的链接?为什么不只发送 id?

标签: jquery ajax struts2


【解决方案1】:

在下面的代码中发送两个字段值我在 fieldValue 属性中连接两个字段值

<s:iterator value="adminSms">
<tr>
<td><s:checkbox name="emails" cssClass="case" fieldValue="%{phone+'@'+email}" /></td>
<td><s:property value="studentname" /></td>
<td id="phone"><s:property value="phone" /></td>
<td id="email"><s:property value="email" /></td>
<td><s:property value="ref" /></td>
</tr>
</s:iterator>

【讨论】:

  • 错了。它不映射 struts 属性。
  • @RomanC 在@符号之间获取电子邮件和电话数据。当谈到行动时,我使用 split 函数来制作单独的数组
  • 只是表达式,映射数据应该是双向的。
猜你喜欢
  • 2017-07-06
  • 2010-09-22
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 2012-01-09
  • 2022-06-17
相关资源
最近更新 更多