gpt4 book ai didi

java - DropDownChoices 返回 null 作为下拉列表中所选项目的值

转载 作者:行者123 更新时间:2023-12-01 14:39:15 26 4
gpt4 key购买 nike

我正在尝试访问用户在 apache wicket 的 DropDownChoices 中选择的值。我将 AjaxFormComponentUpdatingBehavior 添加到 DropDownChoices 元素。我在 onUpdate 行为方法中将 null 作为选定选项。我尝试通过不同方式访问选定的值,如以下代码所示,但在调试时仍然为空。

私有(private)字符串selectedMake;

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

modelsMap.put( "AUDI", Arrays.asList( "A4", "A6", "TT" ) );
modelsMap.put( "CADILLAC", Arrays.asList( "CTS", "DTS", "ESCALADE", "SRX", "DEVILLE" ) );
modelsMap.put( "FORD", Arrays.asList( "CROWN", "ESCAPE", "EXPEDITION", "EXPLORER", "F-150" ) );

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

};

lastDropDownChoice = new DropDownChoice<String>( "daynamicTimeConstrains",
new PropertyModel<String>( this, "selectedMake" ), makeChoices );

lastDropDownChoice.add( new AjaxFormComponentUpdatingBehavior( "onchange" ) {
@Override
protected void onUpdate( AjaxRequestTarget target ) {

// Getting this as null
System.out.println( selectedMake );

// Getting this as null
getFormComponent().getModel().getObject();

}
} );

最佳答案

使用OnChangeAjaxBehavior 。这段代码对我有用:

public class HomePage extends WebPage {

private String text;

public String getText() {
return text;
}

private static final List l = Arrays.asList("a", "b", "c");

public HomePage(final PageParameters parameters) {
super(parameters);
add(ddc());
}

private DropDownChoice ddc(){
DropDownChoice ddc = new DropDownChoice("ddc", new PropertyModel(this, "text"), l);
ddc.add(new OnChangeAjaxBehavior() {

@Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println(getComponent().getDefaultModelObjectAsString());
System.out.println("getText: " + getText());
}
});
return ddc;
}

}

关于java - DropDownChoices 返回 null 作为下拉列表中所选项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16153143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com