VIVEK VIVEK - 5 months ago 22
Ajax Question

Calling an action method through ajax call is behaving unexpectedly

I have an action method given below-

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


I am calling this method via Ajax on dropdown's
onChange
event. After calling this action through Ajax when i run the application on debug mode I see that after executing the action method, this action method is called again and after this another action method is called automatically. this another method is-

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


both action methods are in same action class.
the jQuery method is called only once
onChange
event.The jQuery method is-

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

}


I don't know why it is happening please help.

the entries in
struts.xml
are given below-

<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>

Answer Source

All methods in the action class that have get prefix comply to the JavaBeans convention and could be used as properties of the action bean. This is also called a getter.

The action configuration doesn't have any restrictions to map an action to a method with such name. When the action is called the method is executed, and you have mistaken thinking that the action is called twice.

Methods used as a getter in the action class could be called to access the property. It can be OGNL, JSON or any other code using BeanInfo to access properties and call a getter method. Better not to name methods mapped to the action with a getter to not confuse developers.