如何从Wicket的下拉列表中获取值?

时间:2010-11-19 18:39:47

标签: java ajax wicket

我找到了以下Wicket示例代码:

package org.apache.wicket.examples.ajax.builtin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 * Linked select boxes example
 * 
 * @author Igor Vaynberg (ivaynberg)
 */
public class ChoicePage extends BasePage
{
    private String selectedMake;

    private final Map<String, List<String>> modelsMap = new HashMap<String, List<String>>(); // map:company->model

    /**
     * @return Currently selected make
     */
    public String getSelectedMake()
    {
        return selectedMake;
    }

    /**
     * @param selectedMake
     *            The make that is currently selected
     */
    public void setSelectedMake(String selectedMake)
    {
        this.selectedMake = selectedMake;
    }

    /**
     * Constructor.
     */
    public ChoicePage()
    {
        modelsMap.put("AUDI", Arrays.asList(new String[] { "A4", "A6", "TT" }));
        modelsMap.put("CADILLAC", Arrays.asList(new String[] { "CTS", "DTS", "ESCALADE", "SRX",
                "DEVILLE" }));
        modelsMap.put("FORD", Arrays.asList(new String[] { "CROWN", "ESCAPE", "EXPEDITION",
                "EXPLORER", "F-150" }));

        IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>()
        {
            @Override
            public List<String> getObject()
            {
                Set<String> keys = modelsMap.keySet();
                List<String> list = new ArrayList<String>(keys);
                return list;
            }

        };

        IModel<List<? extends String>> modelChoices = new AbstractReadOnlyModel<List<? extends String>>()
        {
            @Override
            public List<String> getObject()
            {
                List<String> models = modelsMap.get(selectedMake);
                if (models == null)
                {
                    models = Collections.emptyList();
                }
                return models;
            }

        };

        Form<?> form = new Form("form");
        add(form);

        final DropDownChoice<String> makes = new DropDownChoice<String>("makes",
            new PropertyModel<String>(this, "selectedMake"), makeChoices);

        final DropDownChoice<String> models = new DropDownChoice<String>("models",
            new Model<String>(), modelChoices);
        models.setOutputMarkupId(true);

        form.add(makes);
        form.add(models);

        makes.add(new AjaxFormComponentUpdatingBehavior("onchange")
        {
            @Override
            protected void onUpdate(AjaxRequestTarget target)
            {
                target.addComponent(models);
            }
        });
    }
}

假设我有以下课程:

public class car{
      private String name;
      private String  model;

      public setname(String n){
           this.name=n;
      }
      public setModel(String m){
           this.model=m;
      }
      ///  and getters...
}

我想在示例代码中创建一个car对象,并将下拉列表中选择的值分配给car对象。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

您正在寻找PropertyModel class。 Wicket PropertyModel可以让您将组件的值直接绑定到源中的值。 Javadoc示例代码是

Person person = getSomePerson();
add(new Label("myLabel", new PropertyModel(person, "name"));

将该标签添加到页面后,它会在person.name中显示该值,而不会为您提供额外的工作。

您的汽车示例代码已使用PropertyModel,因此您所要做的就是更改目标。例如:

car theCar = new car();
final DropDownChoice<String> makes = new DropDownChoice<String>("makes",
        new PropertyModel<String>(theCar, "name"), makeChoices);
final DropDownChoice<String> models = new DropDownChoice<String>("models",
        new PropertyModel<String>(theCar, "model"), modelChoices);

这会将theCar.name的值设置为制作下拉列表中的内容,将theCar.model的值设置为模型下拉列表中的内容。

编辑:
是的,可以使用按钮而不是自动设置值。为此,请勿使用PropertyModel。而是创建一个新的Wicket Button对象,并使用类似

的代码覆盖其onSubmit()方法
theCar.setName(makes.getValue());
theCar.setModel(models.getValue());

或者,如果您想以AJAX方式执行此操作,请将其放在AjaxFormChoiceComponentUpdatingBehavior的{​​{1}}方法中。

答案 1 :(得分:0)

最好在表单上使用CompoundPropertyModel,在其上添加DropDownChoice组件。

如果你想让DropDownChoice有分区,比如“福特”,“奥迪”等......你可以使用Wicket Select组件,你可以在这里阅读:http://wicket.apache.org/apidocs/1.4/org/apache/wicket/extensions/markup/html/form/select/Select.html

通常,您可以使用dropDownName.getModelObject()或dropDownName.getDefaultModelObject()方法从DropDown获取值。如果它不起作用,你应该试着看看,

System.out.println("something: "+dropDownName.getModelObject());    

显示,该模型可能存在问题。

如果你使用CompoundPropertyModel,你不需要直接从DropDownChoice获取值,但是从CompoundPropertyModel,使用类似的东西:

model.getObject.getMakes();