2012-07-10 90 views
0

我最近开始使用wicket。我试图在书中“Vaynberg的Apache wicket”中的项目示例“Linked select boxes”中使用。Apache wicket - DropDownChioce.onSelectionChanged()不起作用

我重新例子从书:

public class LinkedSelectboxesPage extends WebPage { 

private Country country; 
private City city; 

public LinkedSelectboxesPage() { 
    Database.buildData(); 
    country = Database.getCountries().get(0); 

    FeedbackPanel feedbackPanel = new FeedbackPanel("feedback"); 
    add(feedbackPanel); 

    Form form = new Form("form"); 
    add(form); 

    DropDownChoice<Country> countries = new DropDownChoice<Country>(
      "countries", 
      new PropertyModel<Country>(this, "country"), 
      new CountriesModel(), 
      new ChoiceRenderer<Country>("name", "code")) { 
     protected boolean 
     wantOnSelectionChangedNotifications() { 
      return true; 
     } 

     protected void 
     onSelectionChanged(Country newSelection) { 
      city = null; 
     } 
    }; 
    countries.setRequired(true); 
    form.add(countries); 

    DropDownChoice<City> cities = new DropDownChoice<City>(
      "cities", 
      new PropertyModel<City>(this, "city"), 
      new CitiesModel(), 
      new ChoiceRenderer<City>("name", "code")); 
    cities.setRequired(true); 
    form.add(cities); 
} 

private static class CountriesModel extends LoadableDetachableModel<List<? extends Country>> { 
    protected List<? extends Country> load() { 
     return Database.getCountries(); 
    } 
} 

private class CitiesModel extends LoadableDetachableModel<List<? extends City>> { 
    protected List<? extends City> load() { 
     return Database.getCities(country.getCode()); 
    } 
} 
} 

当我尝试在我的项目,断点,这是固定在函数体前尚未取得使用链接选择盒。

public class AddArticlePanel extends Panel { 

private Type type; 
private Subtype subtype; 

private List<Type> typesList; 
private List<Subtype> subtypesList; 

@SpringBean 
@SuppressWarnings("unused") 
public static ITypeDao typeDao; 

@SpringBean 
@SuppressWarnings("unused") 
private ISubtypeDao subtypeDao; 

public AddArticlePanel(String id) { 
    super(id); 

    this.typesList = typeDao.loadAllTypes(); 
    this.type = this.typesList.get(0); 

    Form form = new Form("form"); 
    add(form); 

    FormComponent<String> tbTitleArticle = new TextField<String>("titleArticle").setRequired(true); 
    form.add(tbTitleArticle); 

    FormComponent<String> taTextArticle = new TextArea<String>("textArticle").setRequired(true); 
    form.add(taTextArticle); 

    DropDownChoice<Type> ddcTypes = new DropDownChoice<Type>(
      "typeArticle", 
      new PropertyModel<Type>(this, "type"), 
      new TypesModel(), 
      new ChoiceRenderer<Type>("name", "id")) { 

     protected boolean wantOnSelectionChangedNotifications() { 
      return true; 
     } 

     protected void onSelectionChanged(Type newSelection) { 
      type = null; 
     } 
    }; 
    ddcTypes.setRequired(true); 
    form.add(ddcTypes); 

    DropDownChoice<Subtype> ddcSubtypes = new DropDownChoice<Subtype>(
      "subtypeArticle", 
      new PropertyModel<Subtype>(this, "subtype"), 
      new SubtypesModel(), 
      new ChoiceRenderer<Subtype>("name", "id")); 
    ddcSubtypes.setRequired(true); 
    form.add(ddcSubtypes); 
} 

public AddArticlePanel(String id, IModel<?> model) { 
    super(id, model); 
} 

private static class TypesModel extends LoadableDetachableModel<List<? extends Type>> { 
    protected List<? extends Type> load() { 
     return typeDao.loadAllTypes(); 
    } 
} 

private class SubtypesModel extends LoadableDetachableModel<List<? extends Subtype>> { 
    protected List<? extends Subtype> load() { 
     return subtypeDao.loadSubtypesByTypeId(type.getId()); 
    } 
} 
} 

我不明白为什么会发生。帮助我,请为我的英语感到抱歉。

+0

尝试在你的onSelectionChanged()中设置子类型(而不是类型).. – bert 2012-07-10 08:02:45

回答

1

在onSelectionChanged方法中,您将类型设置为null。这是支持所需DropDownChoice字段ddcTypes的模型的成员,而不是来自其中countries-ddc onSelectionChanged方法重置城市变量的书中的示例。

在你的情况下,你将每一个类型的改变都重置为null,这样就会使你的表单无效并阻止它做任何有用的事情。

+0

对不起,这个失败,但是当我改变type->子类型它仍然无法正常工作。方法DropDownChoice中的方法“onSelectionChanged()”仍然不会被调用。 – bezdelnick665 2012-07-10 09:45:39

+0

使用Wicket 1.5.7试验了您的代码...像魅力一样工作 – Nicktar 2012-07-10 10:34:12

0

我已经使用wicket 1.5.7。

我使用类AddArticlePanel类AddArticlePage

public class AddArticlePage extends AbstractBasePage { 
    public AddArticlePage() { 
     add(new HeaderPanel("header")); 
     add(new MenuPanel("menu")); 
     add(new AddArticlePanel("body")); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
     add(new FooterPanel("footer")); 
    } 
} 

和类AddArticlePage类BlogApplication使用:

public class BlogApplication extends WebApplication { 

    @Override 
    public void init() { 
     this.getComponentInstantiationListeners().add(new SpringComponentInjector(this)); 
     Injector.get().inject(AddArticlePanel.class); 

     mountPage("/add-article/", AddArticlePage.class); 
    } 

    @Override 
    public Class<? extends Page> getHomePage() { 
     return IndexPage.class; 
    } 
} 

,当我打开网页与链接选择盒构建适应和支持这一不起作用。但如果更改类BlogApplication - >

public class BlogApplication extends WebApplication { 

    @Override 
    public void init() { 
     this.getComponentInstantiationListeners().add(new SpringComponentInjector(this)); 
     Injector.get().inject(AddArticlePanel.class); 

    } 

    @Override 
    public Class<? extends Page> getHomePage() { 
     return AddArticlePage.class; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
    } 
} 

它发生魔术链接选择框将正常工作。也许我不太明白如何/何时/为什么要使用方法WebApplication.mountPage(string,class)。

1

我的解决办法是使用Ajax:

ddcTypes.add(new AjaxFormComponentUpdatingBehavior("onchange") 
{ 
    @Override 
    protected void onUpdate(AjaxRequestTarget target) 
    { 
     target.add(ddcSubtypes); 
    } 
}); 

,但我想知道为什么面板之前没有(没有Ajax)工作...

???? !!!!! !

+0

我认为您需要重写wantOnSelectionChangedNotifications才能使其工作。但是,AjaxFormComponentUpdatingBehavior对我来说更好,因为我可以使用AjaxRequestTarget更轻松地更新页面组件。所以,我会给你一个upvote,因为我最喜欢这个。但是,您的旧问题的答案是重写我想的wantOnSelectionChangedNotifications方法。 – adpro 2017-11-22 14:06:00