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

时间:2012-09-25 10:46:50

标签: java model wicket

如何让面板使用某个modelObject作为父页面?

我有一个使用OwnedAccount作为其模型的表单,并且在表单中我有一个自定义面板,其中包含一个包含financeAccount列表的refreshview。问题是,对于表格的模式对象,不会更改对financeaccounts的更改。

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


}

2 个答案:

答案 0 :(得分:1)

CurrentSelections需要建模,如果它将被两个不同的wicket页面/组件使用和mutiplated。

例如,有一个父页面,它有一个构造函数,一个新的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"));  
       }
}

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

答案 1 :(得分: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();
    }