Hello Timo, thanks for the quick reply.

As you can see I am new to wicket and I'm trying to evaluate it for my projects.
The code I sent in the first email was a bit confusing because I have been 
trying a lot of things to solve my problem.

I have read the wiki page, but I didn't fully understand.
After that I googled for examples and what you saw was my very own wicket 
Frankenstein Monster.

I'll try to explain what happened.


> >        IModel armyChoices = new AbstractReadOnlyModel()
> >        {
> >            public Object getObject()
> >            {
> >                List<Army> armys = new ArrayList<Army>();
> >                armys.add(stwData.getArmyByCategory(StwMapCategory.SMALL));
> >                armys.add(stwData.getArmyByCategory(StwMapCategory.MEDIUM));
> >                armys.add(stwData.getArmyByCategory(StwMapCategory.LARGE));
> >                    
> > stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
> >                return armys;
> >            }
> >        };
> 
> This seems like a strange side effect -- whenever a component
> with armyChoices as its model is rendered,
> stwMatchPrefs.setChosenArmy() is called. Because this is the model
> of the chosenArmy DropDownChoice, whenever the DropDownChoice is
> rendered, this getObject() method is evaluated.


this "stwMatchPrefs.setChosenArmy()" call was not supposed to be here. This was 
another attempt at fixing the problem.



>         mapCategory.add(new AjaxFormComponentUpdatingBehavior("onchange")
>         {
>             protected void onUpdate(AjaxRequestTarget target)
>             {
>                 
> stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
>                 chosenArmy.setModelObject(stwMatchPrefs.getChosenArmy());
>                 target.addComponent(chosenArmy);
>             }
>         });

Here, the "chosenArmy.setModelObject" was also me trying to fix the code.

> BTW, what are these methods here for?

These 2 are there for spring to inject the bean 
"@SpringBean(name="armyServices")"
Also, I have removed the CompoundPropertyModel.

The real problem is that whenever chosenArmy DropDownChoice is changed through 
the AJAX code, stwMatchPrefs.setChosenArmy receives null when the form is 
submitted.
I thought that maybe I had to set the value of "chosenArmy" or "stwMatchPrefs" 
in some place I had not set, so I tried to set these 2 everywhere in the code.
Here's how I should have sent the code in the first email.
It still gives me "null" on submit, after AJAX changes the chosenArmy 
DropDownChoice. 



public class StwFrontPage extends BasePage {
    public ArmyServices armyServices;
    StwData stwData;
    StwMatchPrefs stwMatchPrefs;

    public StwFrontPage() {
        stwMatchPrefs = new StwMatchPrefs();
        Form stwPrefsForm = new StwPrefForm("StwPrefsForm");
        add(stwPrefsForm);

        stwMatchPrefs.setMapCategory(StwMapCategory.SMALL);
        DropDownChoice mapCategory = new DropDownChoice("mapCategory", new 
PropertyModel(stwMatchPrefs, "mapCategory"), StwMapCategory.getList());

        stwPrefsForm.add(mapCategory);

        IChoiceRenderer armyChoicesRenderer = new IChoiceRenderer()
        {
            public Object getDisplayValue(Object o) {
                return ((Army)o).getName();
            }

            public String getIdValue(Object o, int i) {
                return Integer.toString(i);
            }
        };

        stwData = AbismoWicketWebSession.get().getNewAbismoUser().getStwData();
        if(stwData == null){
            stwData = new StwData();
        }
        if(!stwData.isInitialized()){
            stwData.init(AbismoWicketWebSession.get().getNewAbismoUser(),
                        armyServices.getBasicArmy(StwMapCategory.SMALL),
                        armyServices.getBasicArmy(StwMapCategory.MEDIUM),
                        armyServices.getBasicArmy(StwMapCategory.LARGE)
            );
        }
        IModel armyChoices = new AbstractReadOnlyModel()
        {
            public Object getObject()
            {
                List <Army> armys = new ArrayList <Army> ();
                armys.add(stwData.getArmyByCategory(StwMapCategory.SMALL));
                armys.add(stwData.getArmyByCategory(StwMapCategory.MEDIUM));
                armys.add(stwData.getArmyByCategory(StwMapCategory.LARGE));
                return armys;
            }
        };

        
stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(StwMapCategory.SMALL));
        final DropDownChoice chosenArmy = new DropDownChoice("chosenArmy", new 
PropertyModel(stwMatchPrefs, "chosenArmy"), armyChoices, armyChoicesRenderer );
        chosenArmy.setOutputMarkupId(true);
        stwPrefsForm.add(chosenArmy);

        mapCategory.add(new AjaxFormComponentUpdatingBehavior("onchange")
        {
            protected void onUpdate(AjaxRequestTarget target)
            {
                
stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
                target.addComponent(chosenArmy);
            }
        });
    }

    class StwPrefForm extends BaseForm {
        public StwPrefForm(String id) {
            super(id);
        }

        @Override
        public void onSubmit() {
            System.out.println("stwMatchPrefs.getMapCategory() = " + 
stwMatchPrefs.getMapCategory());
            System.out.println("stwMatchPrefs.getChosenArmy() = " + 
stwMatchPrefs.getChosenArmy());
        }
    }
}




----- Original Message ----
From: Timo Rantalaiho <[EMAIL PROTECTED]>
To: [email protected]
Sent: Tuesday, June 24, 2008 12:27:42 AM
Subject: Re: DropDownChoice setting other DropDownChoice using AJAX results in 
null at submit

On Mon, 23 Jun 2008, Giuliano Caliari wrote:
> When the user  sets the value on the first DDC, the value of the second DDC 
> is set without problems, even the selected value is set correctly.
> The problem is that if the user changes the value on the first DDC 
> (mapCategory) and then submits, the value passed on the second DDC 
> (chosenArmy) is always "null", so the value received by "stwMatchPrefs" is 
> null.

Hi, 

I haven't completely understood what is happening here, but 
will try to give some ideas below.

There is something strange happening with the models there.
Have you read the models wiki page? 

You could also try removing PropertyModel and 
CompoundPropertyModel usage for now, because they might 
obfuscate the problem.

>        IModel armyChoices = new AbstractReadOnlyModel()
>        {
>            public Object getObject()
>            {
>                List<Army> armys = new ArrayList<Army>();
>                armys.add(stwData.getArmyByCategory(StwMapCategory.SMALL));
>                armys.add(stwData.getArmyByCategory(StwMapCategory.MEDIUM));
>                armys.add(stwData.getArmyByCategory(StwMapCategory.LARGE));
>                    
> stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
>                return armys;
>            }
>        };

This seems like a strange side effect -- whenever a component
with armyChoices as its model is rendered, 
stwMatchPrefs.setChosenArmy() is called. Because this is the model 
of the chosenArmy DropDownChoice, whenever the DropDownChoice is 
rendered, this getObject() method is evaluated. But then again

>         final DropDownChoice chosenArmy = new DropDownChoice("chosenArmy", 
> new PropertyModel(stwMatchPrefs, "chosenArmy"), armyChoices, 
> armyChoicesRenderer );
>         chosenArmy.setOutputMarkupId(true);
>         stwPrefsForm.add(chosenArmy);
> 
>         mapCategory.add(new AjaxFormComponentUpdatingBehavior("onchange")
>         {
>             protected void onUpdate(AjaxRequestTarget target)
>             {
>                 
> stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
>                 chosenArmy.setModelObject(stwMatchPrefs.getChosenArmy());
>                 target.addComponent(chosenArmy);
>             }
>         });
>     }

Here you give a PropertyModel to chosenArmy DropDownChoice,
and it operates directly with the "chosenArmy" property of 
stwMatchPrefs. This means that the chosenArmy DropDownChoice 
selection and stwMatchPrefs.chosenArmy property are bound to
each other: when one changes, the other one changes as well.

In this light, it is absurd that you change both values in 
the onUpdate method. stwMatchPrefs.setChosenArmy() should be 
enough.

What is even stranger is that because of the armyChoices
model, stwMatchPres.setChosenArmy() is getting a competing 
call during rendering :)

>     public ArmyServices getArmyServices() {
>         return armyServices;
>     }
> 
>     public void setArmyServices(ArmyServices armyServices) {
>         this.armyServices = armyServices;
>     }

BTW, what are these methods here for?

Best wishes,
Timo

-- 
Timo Rantalaiho          
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



      

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to