如何让面板使用某些模型对象作为父页面?
我有一个使用 OwnedAccount 作为模型的表单,在表单中我有一个自定义面板,它有一个包含金融帐户列表的刷新视图。问题是金融帐户的更改没有在表单的模型对象中更改。
一些代码,我删除了很多有 3 个点“...”的代码
@Entity
@Table(name = "ownedaccount")
public class OwnedAccount implements Serializable {
...
//used for multiple currencies
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "ownedAccount")
private List<FinanceAccount> financeAccounts = new ArrayList<FinanceAccount>();
...
}
public class AssetsPage extends LoggedInPage {
...
// bookmarkable constructor
public AssetsPage(PageParameters parameters) {
super(parameters);
init();
}
private void init() {
final OwnedAccount ownedAccount = getCurrentSelections().getSelectedOwnedAccount();
add(new FeedbackPanel("feedback"));
entityEdit = new OwnedAccountForm("entityEdit", ownedAccount);
add(entityEdit);
}
@Override
protected void selectionsChanged() {
OwnedAccount selectedOwnedAccount = getCurrentSelections().getSelectedOwnedAccount();
CompoundPropertyModel<OwnedAccount> model = new CompoundPropertyModel<OwnedAccount>(selectedOwnedAccount);
entityEdit.setModel(model);
}
...
class OwnedAccountForm extends BaseCreateEditForm<OwnedAccount, Void> {
...
public OwnedAccountForm(String s, OwnedAccount entity) {
super(s, entity, null);
assetType = entity.getAssetType();
}
@Override
protected void initComponents(Void initParams) {
...
multipleCurrenciesPanel = new MultipleCurrenciesPanel("multipleCurrenciesPanel", ownedAccountService, getCurrentSelections());
add(multipleCurrenciesPanel);
...
}
...
}
public class MultipleCurrenciesPanel extends Panel {
OwnedAccountService ownedAccountService;
CurrentSelections currentSelections;
public MultipleCurrenciesPanel(String id, OwnedAccountService ownedAccountService, CurrentSelections currentSelections) {
super(id);
this.ownedAccountService = ownedAccountService;
this.currentSelections = currentSelections;
init();
}
private void init() {
DepositoryLabel currencyLabel = new DepositoryLabel("currency", new ResourceModel("currency"));
add(currencyLabel);
DepositoryLabel accountForSecuritasLabel = new DepositoryLabel("account.for.securitas", new ResourceModel("account.for.securitas"));
add(accountForSecuritasLabel);
DepositoryLabel accountForCashLabel = new DepositoryLabel("account.for.cash", new ResourceModel("account.for.cash"));
add(accountForCashLabel);
DepositoryLabel buttonDeleteLabel = new DepositoryLabel("button.delete", new ResourceModel("button.delete"));
add(buttonDeleteLabel);
CurrenciesView currenciesView = new CurrenciesView("financeAccounts", ownedAccountService, currentSelections, this);
add(currenciesView);
setOutputMarkupId(true);
}
}
25/9 更新 - 15:15
public class CurrenciesView extends RefreshingView<FinanceAccount> {
private OwnedAccountService ownedAccountService;
private CurrentSelections currentSelections;
private WebMarkupContainer multipleCurrenciesForDepot;
public CurrenciesView(String id, OwnedAccountService ownedAccountService, CurrentSelections currentSelections, WebMarkupContainer multipleCurrenciesForDepot) {
super(id);
this.ownedAccountService = ownedAccountService;
this.currentSelections = currentSelections;
this.multipleCurrenciesForDepot = multipleCurrenciesForDepot;
}
@Override
protected Iterator getItemModels() {
List<FinanceAccount> financeAccounts = ownedAccountService.getFinanceAccounts(currentSelections.getSelectedOwnedAccount());
return new ModelIteratorAdapter<FinanceAccount>(financeAccounts.iterator()) {
@Override
protected IModel<FinanceAccount> model(FinanceAccount object) {
return new CompoundPropertyModel<FinanceAccount>((object));
}
};
}
@Override
protected void populateItem(Item<FinanceAccount> item) {
final FinanceAccount financeAccount = item.getModelObject();
item.add(new EnumDropDownChoice<MoneyCurrency>("forCurrency"));
item.add(new TextField("accountNumber"));
item.add(new OwnedAccountDropDownChoice("accountForCash", currentSelections.getSelectedLegalEntity().getOwnedBankAccounts()));
item.add(new AjaxButton("deleteFinanceAccount") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
//TODO Delete finance account
ownedAccountService.deleteFinanceAccount(currentSelections.getSelectedOwnedAccount(), financeAccount);
target.add(multipleCurrenciesForDepot);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
//TODO create error message
}
});
}
}