2011-09-25 47 views
4

我試圖用Wicket PageParameters中的密鑰String和值List<object>來添加和獲取。Wicket頁面參數

雖然我的讀取與鍵的值,我得到了classcastException:String cant be converted into list.

我使用的是這樣的:

List<Example> list = (List<Example>)params.get("ExampleList"); 

任何幫助表示讚賞。

+0

你一起工作什麼版本? –

+1

你可以顯示與參數填充對應的代碼片段嗎? –

回答

9

由於PageParameters是HTTP請求參數的抽象,且協議僅支持String值,所以不能存儲PageParameters中的對象。您必須從參數中獲取字符串列表並將其處理爲Example對象。

List<StringValue> values = parameters.getValues("examples"); 
for(StringValue value : values) { 
    Example example = new Example(value.toString()); 
    examples.add(example); 
} 
+0

謝謝。我會試試這個並且標記這個答案。 – jshree

+0

我不明白這個評論中的一些事情。我認爲這一行應該是String values = parameters.get(「examples」);因爲參數get方法返回「string」。我無法分配給列表。即使我從get()mthd獲得String數組,我也不能將字符串對象指定爲Example對象實例。在Example類構造函數中可能不需要「Value.toString」。所以我不知道這個解決方案是否可行。如果我錯了,請讓我知道。 – jshree

+0

我的示例使用代碼Wicket 1.5,其中'PageParameters'的API已更改。 –

0
// Populate PageParameters 
final String dynamicValue = textFieldID.getModelObject();  
PageParameters pageParameters = new PageParameters(); 
pageParameters.add("username", usernameValue); 
pageParameters.add("username", "fixedValue"); 

// Retrieving PageParameters 
String newValue = parameters.getValues("username").get(1).toString(); 
// here newValue will contain "fixedValue" (the second element)