【问题标题】:Calling an action method through ajax call is behaving unexpectedly通过 ajax 调用调用动作方法的行为异常
【发布时间】:2014-10-01 19:03:52
【问题描述】:

我有一个下面给出的操作方法-

public String getCommissionaryOfficeByCustomLocation() {
    Connection conn = null;

    try {
        ApplicantDbMethods db = new ApplicantDbMethods();
        conn = db.getConnection();
        commissionaryOffice = db.getCommissionaryOffice(conn, selectedCustomLocation);
        return SUCCESS;
    } catch (Exception ex) {
        return ERROR;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ApplicantRegistrationDetails.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

我通过 Ajax 在下拉列表的 onChange 事件上调用此方法。当我在调试模式下运行应用程序时通过 Ajax 调用此操作后,我看到执行操作方法后,再次调用此操作方法,然后自动调用另一个操作方法。这另一种方法是-

public String getContactPersonForFutureCommunications() {
    Connection conn = null;
    try {
        session = ActionContext.getContext().getSession();
        if (session == null) {
            return ERROR;
        }
        String applicantId = session.get("ApplicantId").toString();
        if (applicantId == null) {
            return ERROR;
        }
        ApplicantDbMethods db = new ApplicantDbMethods();
        conn = db.getConnection();
        // db.insertFutureContactPerson(conn,applicantId,futureContact);
        if ("Other".equals(futureContact)) {
            return "OTHER";
        }
        return SUCCESS;
    } catch (Exception ex) {
        return ERROR;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ApplicantRegistrationDetails.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

两个动作方法都在同一个动作类中。 jQuery 方法只调用一次onChange 事件。jQuery 方法是-

function getCommissionaryOffice(customLocation) {
    var location = $('#' + customLocation).val();
    $.ajax(
            {
                type: 'POST',
                url: 'getCommissionaryOffice',
                data: {selectedCustomLocation: location},
                //async: false ,
                success: function(data) {
                    var commissionaryOffice=data.commissionaryOffice;
                    $('#commissionaryOffice').val(commissionaryOffice);
                },
                error:function(data){
                    alert("error getting commissionay office!");
                }
            });

}

我不知道为什么会这样,请帮忙。
struts.xml 中的条目如下-

<package name="default" extends="json-default">
    <result-types>
        <result-type name="tiles"
                     class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>

    <action name="getCommissionaryOffice" class="applicant.ApplicantRegistrationDetails" method="getCommissionaryOfficeByCustomLocation">
        <result name="success" type="json"/>
    </action>

    <action name="FutureContactPerson" class="applicant.ApplicantRegistrationDetails" method="getContactPersonForFutureCommunications">
         <result name="input" type="tiles">FutureContactDetails</result>
        <result name="success" type="tiles">SuccessfullySubmitted</result>

    </action>
 </package> 

【问题讨论】:

  • 这是因为您的方法名称以get 开头,json 结果会尝试对其进行序列化。
  • @AleksandrM 是的,问题已解决,但请您详细说明一下这种行为。
  • 它可能对其他人也有用
  • 没有什么可详细说明的。阅读文档:struts.apache.org/release/2.3.x/docs/json-plugin.html
  • 好的,谢谢参考。

标签: java jquery ajax json struts2


【解决方案1】:

动作类中所有具有get 前缀的方法都符合JavaBeans 约定,并且可以用作动作bean 的属性。这也称为 getter

动作配置对于将动作映射到具有此类名称的方法没有任何限制。当动作被调用时,方法被执行,你错误地认为动作被调用了两次。

可以调用在动作类中用作 getter 的方法来访问该属性。它可以是OGNLJSON 或任何其他使用BeanInfo 访问属性并调用getter 方法的代码。最好不要使用 getter 命名映射到操作的方法,以免混淆开发人员。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多