2012-07-10 100 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