Wicket:如何从子类覆盖 onSubmit 方法?
Wicket: How to override onSubmit method from subclass?
我有一个超类 A 和一个子类 B。超类 A 有一个表单和 onSubmit 方法。我想在子类上覆盖该表单方法。如何实现?
示例 A
form.add(new AjaxFormSubmitBehavior(form, "onsubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
super.onSubmit(target);
if (errorsList == null) errorsList = new ArrayList<>();
prepareFormValuesForSubmit();
PaymentsValidator.debtInfoIsValid(model.getObject(), errorsList);
if (!errorsList.isEmpty()) {
for (String error : errorsList)
error(error);
JavascriptUtils.showMessagesForFeedbackPanel(target, feedback);
return;
}
Long id = sendPayment(target);
getOBSession().setAttribute((Object) DebtInfoViewPage.PAGE_PARAMETER, id);
JavascriptUtils.goToPage(target, Constants.DEBTINFO_BASE_URL + "/" + DebtInfoViewPage.class.getSimpleName());
getOBSession().setSaveDoc(false);
}
在 B 上:
getPaymentForm().add(new AjaxFormSubmitBehavior(getPaymentForm(), "onSubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
List<String> errors = new ArrayList<>();
PaymentsValidator.auditStatementIsValid(auditorsContactModel.getObject(), errors);
if (errors.size() == 0) {
auditorsContactModel.getObject().setDebtInfoId(getDebtInfoModel().getObject().getId());
ServiceLocator.getService(DocumentService.class).persist(auditorsContactModel.getObject());
}
}
});
那个方法不行,好像是在添加另一个onSubmit。
您需要在 A.java 中创建 onSubmit(Form<?> form, AjaxRequestTarget target)
并委托给它:
A.java:
form.add(new AjaxFormSubmitBehavior(form, "onsubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
super.onSubmit(target);
/*A.this.*/onSubmit(form, target);
}
});
protected void onSubmit(Form<?> form, AjaxRequestTarget target) {
if (errorsList == null) errorsList = new ArrayList<>();
prepareFormValuesForSubmit();
PaymentsValidator.debtInfoIsValid(model.getObject(), errorsList);
if (!errorsList.isEmpty()) {
for (String error : errorsList)
error(error);
JavascriptUtils.showMessagesForFeedbackPanel(target, feedback);
return;
}
Long id = sendPayment(target);
getOBSession().setAttribute((Object) DebtInfoViewPage.PAGE_PARAMETER, id);
JavascriptUtils.goToPage(target, Constants.DEBTINFO_BASE_URL + "/" + DebtInfoViewPage.class.getSimpleName());
getOBSession().setSaveDoc(false);
}
B.java:
@Override void onSubmit(Form<?> form, AjaxRequestTarget target) { {
List<String> errors = new ArrayList<>();
PaymentsValidator.auditStatementIsValid(auditorsContactModel.getObject(), errors);
if (errors.size() == 0) {
auditorsContactModel.getObject().setDebtInfoId(getDebtInfoModel().getObject().getId());
ServiceLocator.getService(DocumentService.class).persist(auditorsContactModel.getObject());
}
}
我有一个超类 A 和一个子类 B。超类 A 有一个表单和 onSubmit 方法。我想在子类上覆盖该表单方法。如何实现?
示例 A
form.add(new AjaxFormSubmitBehavior(form, "onsubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
super.onSubmit(target);
if (errorsList == null) errorsList = new ArrayList<>();
prepareFormValuesForSubmit();
PaymentsValidator.debtInfoIsValid(model.getObject(), errorsList);
if (!errorsList.isEmpty()) {
for (String error : errorsList)
error(error);
JavascriptUtils.showMessagesForFeedbackPanel(target, feedback);
return;
}
Long id = sendPayment(target);
getOBSession().setAttribute((Object) DebtInfoViewPage.PAGE_PARAMETER, id);
JavascriptUtils.goToPage(target, Constants.DEBTINFO_BASE_URL + "/" + DebtInfoViewPage.class.getSimpleName());
getOBSession().setSaveDoc(false);
}
在 B 上:
getPaymentForm().add(new AjaxFormSubmitBehavior(getPaymentForm(), "onSubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
List<String> errors = new ArrayList<>();
PaymentsValidator.auditStatementIsValid(auditorsContactModel.getObject(), errors);
if (errors.size() == 0) {
auditorsContactModel.getObject().setDebtInfoId(getDebtInfoModel().getObject().getId());
ServiceLocator.getService(DocumentService.class).persist(auditorsContactModel.getObject());
}
}
});
那个方法不行,好像是在添加另一个onSubmit。
您需要在 A.java 中创建 onSubmit(Form<?> form, AjaxRequestTarget target)
并委托给它:
A.java:
form.add(new AjaxFormSubmitBehavior(form, "onsubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
super.onSubmit(target);
/*A.this.*/onSubmit(form, target);
}
});
protected void onSubmit(Form<?> form, AjaxRequestTarget target) {
if (errorsList == null) errorsList = new ArrayList<>();
prepareFormValuesForSubmit();
PaymentsValidator.debtInfoIsValid(model.getObject(), errorsList);
if (!errorsList.isEmpty()) {
for (String error : errorsList)
error(error);
JavascriptUtils.showMessagesForFeedbackPanel(target, feedback);
return;
}
Long id = sendPayment(target);
getOBSession().setAttribute((Object) DebtInfoViewPage.PAGE_PARAMETER, id);
JavascriptUtils.goToPage(target, Constants.DEBTINFO_BASE_URL + "/" + DebtInfoViewPage.class.getSimpleName());
getOBSession().setSaveDoc(false);
}
B.java:
@Override void onSubmit(Form<?> form, AjaxRequestTarget target) { {
List<String> errors = new ArrayList<>();
PaymentsValidator.auditStatementIsValid(auditorsContactModel.getObject(), errors);
if (errors.size() == 0) {
auditorsContactModel.getObject().setDebtInfoId(getDebtInfoModel().getObject().getId());
ServiceLocator.getService(DocumentService.class).persist(auditorsContactModel.getObject());
}
}