gpt4 book ai didi

java - 通过 ajax 调用调用操作方法时出现意外行为

转载 作者:行者123 更新时间:2023-11-30 11:15:47 25 4
gpt4 key购买 nike

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

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);
}
}
}
}

我在下拉列表的 onChange 事件上通过 Ajax 调用此方法。当我在 Debug模式下运行应用程序时通过 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);
}
}
}
}

两个 Action 方法都在同一个 Action 类中。 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 前缀的方法都符合JavaBeans约定,并且可以用作 Action bean 的属性。这也称为 getter

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

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

关于java - 通过 ajax 调用调用操作方法时出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25201142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com