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

发布于 2024-10-03 01:48:39 字数 3732 浏览 2 评论 0原文

我找到了以下 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对象。我怎样才能做到这一点?

I've found the following Wicket sample code:

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

Suppose I have the following class:

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...
}

I want to create a car object in the sample code, and assign the values selected in the dropdown to the car object. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-10-10 01:48:39

您正在寻找 PropertyModel。 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 中onUpdate() 方法。

You're looking for the PropertyModel class. Wicket PropertyModels can let you tie the value of a component directly to a value in your source. The Javadoc sample code is

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

When that label is added to the page, it'll display the value in person.name with no extra work on your end.

Your car sample code already uses PropertyModels, so all you have to do is change the target. For example:

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

This will set the value of theCar.name to what's in the makes dropdown list and the value of theCar.model to what's in the models dropdown list.

EDIT:
Yes, it's possible to set the values with a button instead of automatically. To do this, don't use the PropertyModels. Instead, create a new Wicket Button object and override its onSubmit() method with code like

theCar.setName(makes.getValue());
theCar.setModel(models.getValue());

Or, if you want to do it AJAXically, put that inside an AjaxFormChoiceComponentUpdatingBehavior's onUpdate() method.

小糖芽 2024-10-10 01:48:39

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

如果您想要具有划分区域的 DropDownChoice,例如“Ford”、“Audi”等。您可以使用 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();

It is better to use CompoundPropertyModel on form, on which you add DropDownChoice components.

If you would like to have DropDownChoice with divided areas, like "Ford", "Audi" etc.. you can use Wicket Select component, you can read about it here: http://wicket.apache.org/apidocs/1.4/org/apache/wicket/extensions/markup/html/form/select/Select.html .

And usually you can get value from DropDown using dropDownName.getModelObject() or dropDownName.getDefaultModelObject() methods. If it is not working, you should try to see, what

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

shows, maybe there are problems with the model.

In case you use CompoundPropertyModel, you do not need to get value directly from DropDownChoice, but from CompoundPropertyModel, use something like:

model.getObject.getMakes();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文