我有以下给出的操作方法-
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调用此方法。当我在调试模式下运行应用程序时通过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
前缀的方法均符合JavaBeans约定,并且可以用作动作bean的属性。这也称为吸气剂。
操作配置没有任何限制将操作映射到具有此类名称的方法。调用动作时,将执行该方法,并且您误以为该动作被调用了两次。
可以调用操作类中用作获取方法的方法来访问属性。它可以是OGNL,JSON或使用BeanInfo
来访问属性并调用getter方法的任何其他代码。最好不要使用getter来命名映射到操作的方法,以免混淆开发人员。