0

如何让面板使用某些模型对象作为父页面?

我有一个使用 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
            }
        });
    }


}
4

2 回答 2

1

CurrentSelections needs to model if its going to be used and mutiplated by two different wicket page/components.

For example theres a parent page, which has a constructor, a new String object and a Panel that uses the String object as a parameter,

public class ParentPage extends WebPage {

       public ParentPage() {

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

If the string object is updated after the panel has been added, then that updated string isn't what the panel has. What the panel has is "Dave" when thought the ParentPage now has the string as "Brian".

But if we made a model and made it use the string object then when we change the string the childPanel will get the update.

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

Its a very simple example but i hope it helps.

于 2012-09-25T13:53:30.857 回答
0

我将构造函数中传递的模型引用保存到面板,到目前为止它工作正常。

 BankAccountPanel bankAccountPanel = new BankAccountPanel("bankAccountPanel", getModel());
            add(bankAccountPanel);

...

public class BankAccountPanel extends Panel {

    IModel<OwnedAccount> iModel;

    public BankAccountPanel(String id, IModel<OwnedAccount> model) {
        super(id, model);
        this.iModel = model;
        init();
    }
于 2012-09-28T06:54:27.467 回答