Java DropDownChoices正在将null作为下拉列表中选定项的值返回

Java DropDownChoices正在将null作为下拉列表中选定项的值返回,java,wicket,Java,Wicket,我试图访问用户在ApacheWicket的DropDownChoices中选择的值。 我正在向DropDownChoices元素添加AjaxFormComponentUpdateBehavior。在onUpdate行为方法中,我将选择null作为选项。我试图通过如下代码所示的不同方式访问所选值,但在去毛刺时仍然为null 私有字符串selectedMake private final Map<String, List<String>> modelsMap = new Ha

我试图访问用户在ApacheWicket的DropDownChoices中选择的值。 我正在向DropDownChoices元素添加AjaxFormComponentUpdateBehavior。在onUpdate行为方法中,我将选择null作为选项。我试图通过如下代码所示的不同方式访问所选值,但在去毛刺时仍然为null

私有字符串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();

        }
    } );
private final Map modelsMap=new HashMap();//地图:公司->模型
模型映射放置(“奥迪”,数组.asList(“A4”,“A6”,“TT”);
modelsMap.put(“凯迪拉克”,Arrays.asList(“CTS”,“DTS”,“ESCALADE”,“SRX”,“DEVILLE”));
模型映射放置(“福特”,数组。asList(“皇冠”,“逃生”,“探险”,“探险家”,“F-150”);

IModel我过去对此有过一些问题,主要是文本框,因为在提交表单之前输入不会被处理。也就是说,当属性模型似乎没有正确更新时,我将使用getFormComponent().getConvertedInput()并在AjaxFormComponentUpdateingBehavior()中强制转换它,如下所示:

lastDropDownChoice.add( new AjaxFormComponentUpdatingBehavior( "onchange" ) {
    @Override
    protected void onUpdate( AjaxRequestTarget target ) {
        String input = (String) getFormComponent().getConvertedInput();
        System.out.println( input );
        selectedModel = input;
    }
} );
我很想从其他人那里听到这种方法可能有哪些缺点,但它似乎比人们可以使用的许多其他看似潜在的选项更可靠

顺便说一句,我通常会创建一个新的ICHOICerender()并重写抽象值,以便能够传递对象而不是字符串,并且能够按照我希望的方式精确显示它们,如下所示:

lastDropDownChoice = new IndicatingDropDownChoice("daynamicTimeConstrains", new PropertyModel(this, "selectedMake"), makeList, new IChoiceRenderer() {
    @Override
    public Object getDisplayValue(Object o) {
        return makeList.indexOf(selectedMake) + "." + o.toString();
    }

    @Override
    public String getIdValue(Object o, int i) {
        return o.toString();
    }
});
使用。此代码适用于我:

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

}

对我有用。您看到下拉列表的值实际被发布了吗?除了删除AjaxFormComponentUpdateBehavior的onUpdate()方法外,我如何检查实际发布的内容?只需在FireBug(或类似工具)中检查您在下拉列表中做出选择后发生了什么。(Firebug->网络选项卡);您应该会看到类似于参数“DaynamicTimeConstraints”、“0”的内容,但代码仍然是开箱即用的。因此,找出导致代码失败的先决条件会更好。如果getConvertedInput()返回非null值,则可能意味着验证失败,因此未设置模型。