2012-09-01 35 views
2

我在我的web應用程序中使用選擇框創建表單。我以下面描述的方式使用DropDownChoice + ChoiceRenderer組合。它工作正常,但有一件事 - 在不預先選擇默認值。Wicket - DropDownChoice + ChoiceRenderer - 預先選擇不起作用

我一直在爲此奮鬥了很長時間。我在互聯網上發現了一些據說可行的例子,但它們並不適合我。

我的基本代碼看起來像這樣(不相關的部分已經被省略或簡化,以提高清晰度):

業務實體

public class MultivalueType 
{ 
    private Long id; 
    private String label; 
    private String description; 

    public MultivalueType(Long id, String label, String description) 
    { 
     this.id = id 
     this.label = label; 
     this.description = description; 
    } 

    public Long getId() 
    { 
     return id; 
    } 

    public String getLabel() 
    { 
     return label; 
    } 

    public String getDescription() 
    { 
     return description; 
    } 
} 

public class PropertySettings 
{ 
    private Long id; 
    private String property; 
    private MultivalueType multivalueType; 

    public PropertySettings(Long id, String property, MultivalueType multivalueType) 
    { 
     this.id = id; 
     this.property = property; 
     this.multivalueType = multivalueType; 
    } 

    public MultivalueType getMultivalueType() 
    { 
     return multivalueType; 
    } 
} 

DAO

public class PropertySettingsDao 
{ 
    ... 
    @Override 
    public PropertySettings load(Long id) 
    { 
     String query = " ... query ... "; 
     Object[] params = { id }; 
     return getJdbcTemplate().queryForObject(query, params, getRowMapper()); 
} 
    ... 
} 

Form類

PropertySettings property = propertySettingsDao.load(propertyId); 
IModel<PropertySettings> formModel = new CompoundPropertyModel<PropertySettings>(property); 

Form<PropertySettings> form = new Form<PropertySettings>("editPropertyForm", formModel) 
{ 
    @Override 
    protected void onSubmit() 
    { 
     PropertySettings propertySettings = this.getModelObject(); 
     ...   
    } 
}; 

form.add(createEnumSelectbox(multivalueTypeDao, new PropertyModel<MultivalueType>(property, "multivalueType"), "multivalueType", true)); 

add(form); 

創建選擇框

protected DropDownChoice<MultivalueType> createEnumSelectbox(DaoForEntityWithSurrogateKey<MultivalueType> dao, IModel<MultivalueType> model, String componentName, boolean required) 
{ 
    // create the model 
    IModel<List<MultivalueType>> choices = createModelForListView(dao); 

    // prepare the select-box renderer 
    ChoiceRenderer<MultivalueType> renderer = new ChoiceRenderer<MultivalueType>("label", "id"); 

    // create the select-box component 
    DropDownChoice<MultivalueType> selectBox = new DropDownChoice<MultivalueType> 
    (
     componentName, 
     model, 
     choices, 
     renderer 
    ); 

    // mark the select-box as a required form field 
    selectBox.setRequired(required); 

    return selectBox; 
} 

protected IModel<List<MultivalueType>> createModelForListView(final DaoForEntityWithSurrogateKey<MultivalueType> dao) 
{ 
    return new LoadableDetachableModel<List<MultivalueType>>() 
    { 
     @Override 
     protected List<BO> load() 
     { 
      return dao.loadAll(); 
     } 
    }; 
} 

注意,我設置默認MultivalueType實例(冗餘)都作爲表單組件的CompundPropertyModel並直接作爲DropDownChoice組件模型。根據我在互聯網上找到的信息,這應該足以讓選擇框在頁面加載時預先選擇相應的值,但不起作用。

我已經驗證表單組件中的屬性變量(從DAO獲得)確實包含有效數據。

另外請注意,一切工作正常,但預選 - 我在窗體的onSubmit方法正確填充propertySettings變量。

回答

5

我終於找到了問題。

我意識到,當我展示的形式,檢票記錄以下警告:

Model returned object ... which is not available in the list of selected objects. 

基礎上,我發現了錯誤信息,爲了對的默認選項的預選工作,業務實體必須重寫equals方法(請參閱corresponding JIRA ticket)。至少在我使用的Wicket版本1.5.4中。

希望這可以幫助那些會陷入同樣問題的人!

+0

你明白了!我也可以建議你使用PropertySettings的LoadableDetachableModel實現?如果多值類型中的描述很長,它會爲您節省一些內存。然後,您可以將該模型實例用作表單,並將其作爲您爲選擇框的選定選項實例化的PropertyModel的嵌套模型。看看這裏找到原因 - https://cwiki.apache.org/WICKET/page-maps.html此外,使用LoadableDetachableModel將確保您的頁面的所有版本都使用最新的實例屬性設置。 – buritos

+0

謝謝你的建議。你是對的,我寧願使用LoadableDetachableModel作爲表單模型。 –