2012-05-19 22 views
2

在我的行動類我想要一個字符串的地圖。而在我的tml中,我想用textfield訪問這張地圖。像綁定掛圖5

<t:form> 
    <t:textfield value="myMap['key1']"/> 
    <t:textfield value="myMap['key2']"/> 
... 

我不堅持語法,但有什麼這樣的事情,目前在tapestry?如果沒有,我需要以最簡單的方式創建這種轉換?類型強制?自定義組件?我開始學習掛毯,所以請隨意詳細:)

回答

5

另一種選擇是綁定自己的tml前綴。有一個綁定前綴here的示例。

我們寫我們自己的前綴map這使得我們在TML這樣得到的數值:

${map:myMap.key1} 
+0

thx中的鍵的條目!比我的解決方案好得多。但需要更多的工作:( – piotrek

2

您將需要在您的java類中創建一個存取方法。

最簡單的方法是添加一個方法:

getMapValue(String key){...}

你就可以改變你的TML使用

value="getMapValue('key1')"

+0

這種方式,只讀,所以我不能在文本字段中使用它,我可以?文本字段需要一個帶有單個參數的setter - 值。所以沒有地方通過密鑰 – piotrek

1

您應該能夠遍歷按鍵設置如下:

<form t:type="Form"> 
    <t:Loop t:source="myMap.keySet()" t:value="currentKey"> 
     <input type="text" t:type="Textfield" t:value="currentValue"/> 
    </t:Loop> 
</form> 

你必須在存儲當前的地圖項,並將獲得的電流值的類文件添加一些代碼:(這個答案是從我earlier answers一個改編)

@Property 
private Object currentKey; 

@Persist 
@Property 
private Map<String,String> myMap; 

public String getCurrentValue() { 
    return this.myMap.get(this.currentKey); 
} 

public void setCurrentValue(final String currentValue) { 
    this.myMap.put(this.currentKey, currentValue); 
} 

+0

這種方式,我必須遍歷整個地圖,即使我只想要幾個鍵。我也不能添加不在地圖 – piotrek

2

好吧,我想通了。我做了一個簡單的組件MapField可:

@Parameter(required=true) 
Map<String, String> map; 

@Parameter(required=true, allowNull=false, defaultPrefix = BindingConstants.LITERAL) 
String key; 

public String getMapValue() { 
    return map.get(key); 
} 

public void setMapValue(String value) { 
    map.put(key, value); 
} 

TML:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"> 
    <t:textfield value="mapValue"/> 
</html> 

就是這樣。現在我們可以在其他TML使用它:

<t:mapField key="existingOrNot" t:map="myMap"/> 

,並在頁面中我們只myMap需要作爲一個屬性:

@Property @Persist Map<String, String> myMap; 

可能還有更多的事情要做,像傳遞所有其他HTML參數到文本框等