奇葛格的BLOG

红尘最可笑,我自乐逍遥

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  59 随笔 :: 23 文章 :: 11 评论 :: 0 Trackbacks
In the following code:

            add(new Button("saveButton")

            {
                public void onSubmit()
                {
                    //save form values, redirect
@@                    Product product = (Product)getModelObject();
                    ProductProxy.addProduct(product);
                    setResponsePage(new EditProduct());
                }
            });

The line marked @@ is wrong. You ask from the submit button the modelobject. Because the button doesn't have a model (model == null), and the form has a compound model, it will use the component id "saveButton" as an OGNL expression with the form modelobject as base.

This happened because you create an anonymous subclass on the Button. The event handler 'onSubmit' has a this pointer to the button.

This can be solved by changing the line to the following:
Product product = (Product)getParent().getModelObject();

Martijn


On 3/20/06, Vincent Jenks <scientifik.com@...> wrote:
OK, believe it or not, I did just that and *still* cannot get it to work.

Here's my Form class:

    private static class EditProductForm extends Form
    {
        public EditProductForm(String name, Product product)
        {
            super(name, new CompoundPropertyModel(product));
           
            //get collection of Category items
            IModel catsModel = new LoadableDetachableModel()
            {
                protected Object load()
                {
                    return ProductProxy.getAllCategories(); //via proxy
                }
            };
           
            //add form components
            add(new TextField("productCode").add(RequiredValidator.getInstance()));
            add(new TextField("name").add(RequiredValidator.getInstance()));
            add(new TextArea("summary").add( RequiredValidator.getInstance()));
            add(new TextArea("description").add(RequiredValidator.getInstance()));
            add(new TextField("unitPrice"));
            add(new TextField("weight"));
            add(new TextField("height"));
            add(new TextField("width"));
            add(new TextField("length"));
            add(new TextField("insuredValue"));
            add(new CheckBox("freeShipping"));
            add(new TextField("thumbnail").add(RequiredValidator.getInstance()));
            add(new TextField("photo").add(RequiredValidator.getInstance ()));
           
            //add reset button
            add(new Button("cancelButton")
            {
                public void onSubmit()
                {
                   
                    setResponsePage(new EditProduct());
                }
            }.setDefaultFormProcessing(false));
           
            //add reset button
            add(new Button("saveButton")
            {
                public void onSubmit()
                {
                    //save form values, redirect
                    Product product = (Product)getModelObject();
                    ProductProxy.addProduct(product);
                    setResponsePage(new EditProduct());
                }
            });
        }
    }

The cancel button works now but the save button is now throwing an exception:

wicket.WicketRuntimeException : Method public abstract void wicket.markup.html.form.IFormSubmitListener.onFormSubmitted () of interface java.lang.reflect.Method threw an exception ...
...
Caused by: java.lang.reflect.InvocationTargetException ...
...
Caused by: wicket.WicketRuntimeException: OGNL Exception: expression='saveButton'; path='4:editProductForm:saveButton'
...
Caused by: ognl.NoSuchPropertyException: com.myapp.model.Product.saveButton
...

Here's the form HTML just in case I'm goofing something there:

<form wicket:id="editProductForm" id="editProductForm">
...
                                    <tr>
                                        <td colspan="2">
                                            <input type="submit" wicket:id="saveButton" value="Save" />
                                            &nbsp;
                                            <input type="submit" wicket:id="cancelButton" value="Cancel" />
                                            <br />
                                            <br />
                                        </td>
                                    </tr>
                                    </form>

What's wrong w/ this?

Thanks!
posted on 2006-09-11 15:24 奇葛格 阅读(706) 评论(0)  编辑  收藏 所属分类: Wicket

只有注册用户登录后才能发表评论。


网站导航: