我正在尝试访问用户在apache wicket的DropDownChoices中选择的值。
我将AjaxFormComponentUpdatingBehavior添加到DropDownChoices元素。我在行为的onUpdate方法中将null作为选定选项。我尝试通过以下代码中所示的不同方式访问选定的值,但是在调试时仍然会为null。
private String 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/