gpt4 book ai didi

java - 面板使用与父页面相同的模型

转载 作者:行者123 更新时间:2023-12-01 15:08:52 25 4
gpt4 key购买 nike

如何获得一个面板来使用某个 modelObject 作为父页面?

我有一个使用 OwnedAccount 作为其模型的表单,并且在该表单中我有一个自定义面板,其中包含包含 FinanceAccount 列表的刷新 View 。问题是对财务帐户的更改并未在表单的模型对象中更改。

一些代码,我删除了很多有 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
}
});
}


}

最佳答案

如果 CurrentSelections 将被两个不同的 wicket 页面/组件使用和修改,则需要对其进行建模。

例如,有一个父页面,它有一个构造函数、一个新的 String 对象和一个使用 String 对象作为参数的 Panel,

public class ParentPage extends WebPage {

public ParentPage() {

String string = new String("Dave");
add(new Panel("childPanel", string));
string = new String("Brian");
}
}

如果在添加面板后更新字符串对象,则更新后的字符串不是面板所具有的字符串。当认为 ParentPage 现在的字符串为“Brian”时,面板显示的是“Dave”。

但是如果我们创建了一个模型并使其使用字符串对象,那么当我们更改字符串时,childPanel 将获得更新。

public class ParentPage extends WebPage {

public ParentPage() {

String string = new String("Dave");
IModel model = new Model(string);

add(new Panel("childPanel", model));
model.setObject(new String("Brian"));
}
}

这是一个非常简单的例子,但我希望它有帮助。

关于java - 面板使用与父页面相同的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12581370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com