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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4228260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:24:36  来源:igfitidea点击:

How do you get values from a dropdown list in Wicket?

javaajaxwicket

提问by ajdar ajdaro?lu

I've found the following Wicket sample code:

我找到了以下 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);
            }
        });
    }
}

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 carobject in the sample code, and assign the values selected in the dropdown to the carobject. How can I do that?

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

回答by Pops

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

您正在寻找PropertyModel班级。WicketPropertyModel可以让您将组件的值直接与源中的值联系起来。Javadoc 示例代码是

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.namewith no extra work on your end.

当该标签添加到页面时,它会显示值,person.name而无需您进行额外的工作。

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

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

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.nameto what's in the makes dropdown list and the value of theCar.modelto what's in the models dropdown list.

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

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 Buttonobject and override its onSubmit()method with code like

编辑:
是的,可以使用按钮而不是自动设置值。为此,请不要使用PropertyModels。相反,创建一个新的 WicketButton对象并onSubmit()使用类似的代码覆盖其方法

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

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

或者,如果您想以 AJAX 方式执行此操作,请将其放在 anAjaxFormChoiceComponentUpdatingBehavioronUpdate()方法中。

回答by Gytis

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

最好在窗体上使用 CompoundPropertyModel,在窗体上添加 DropDownChoice 组件。

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.

如果您想要具有分割区域的 DropDownChoice,例如“福特”、“奥迪”等。您可以使用 Wicket Select 组件,您可以在此处阅读: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

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

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:

如果您使用 CompoundPropertyModel,则不需要直接从 DropDownChoice 获取值,而是从 CompoundPropertyModel 中获取值,请使用以下内容:

model.getObject.getMakes();