如何从 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对象。我怎样才能做到这一点?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
PropertyModel
类。 WicketPropertyModel
可以让您将组件的值直接绑定到源中的值。 Javadoc 示例代码是当该标签添加到页面时,它将显示
person.name
中的值,而无需您进行额外的工作。您的汽车示例代码已使用
PropertyModel
,因此您所要做的就是更改目标。例如:这会将
theCar.name
的值设置为品牌下拉列表中的值,将theCar.model
的值设置为型号下拉列表中的值。编辑:
是的,可以使用按钮而不是自动设置值。为此,请勿使用
PropertyModel
。相反,创建一个新的 WicketButton
对象并使用类似代码覆盖其onSubmit()
方法,或者,如果您想以 AJAX 方式执行此操作,请将其放入
AjaxFormChoiceComponentUpdatingBehavior 中
的onUpdate()
方法。You're looking for the
PropertyModel
class. WicketPropertyModel
s can let you tie the value of a component directly to a value in your source. The Javadoc sample code isWhen 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
PropertyModel
s, so all you have to do is change the target. For example:This will set the value of
theCar.name
to what's in the makes dropdown list and the value oftheCar.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
PropertyModel
s. Instead, create a new WicketButton
object and override itsonSubmit()
method with code likeOr, if you want to do it AJAXically, put that inside an
AjaxFormChoiceComponentUpdatingBehavior
'sonUpdate()
method.最好在表单上使用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 获取值。如果它不起作用,你应该尝试看看,
显示了什么,也许模型有问题。
如果您使用CompoundPropertyModel,则不需要直接从DropDownChoice获取值,而是从CompoundPropertyModel获取值,例如:
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
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: