【发布时间】: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