基本問題就是生成一個HTML表單。你似乎已經想出了你的模型。你缺少的是一個觀點。我做了一次類似以下的事情,爲simpleDB模型生成一個模型。
我提供了一個字段列表,並根據這些字段生成UI。我只有文本字段,只需要2個(可見和不可見)字段。您的用例可能需要更多的複雜性,所以您可以根據需要進行調整。
dish.fields
包含具有相關元數據的字段。任何特殊的事情,如需要驗證,或isRequired,你將不得不提供該信息的視圖,所以它可以呈現適當的方式領域。
最簡單的建模方法是從一個HTML表單開始,並開始一次將其概括爲一個字段。
#{list items:dish.fields, as:'f'}
#{field 'f'}
#{if f.display }
<div class="control-group">
<label class="control-label"> &{f.name} </label>
<div class="controls">
<input type="text" class="input-xlarge" placeholder="&{f.name}" name="${dish.getFieldId(f)}" value="${dish.getValue(f)}" ></input>
</div>
</div>
#{/if}
#{else}
<input type="hidden" class="input-xlarge" placeholder="&{f.name}" name="${dish.getFieldId(f)}" value="${dish.getValue(f)}" ></input>
#{/else}
#{/field}
#{/list}
#{else}
No fields
#{/else}
我不得不定義自己的領域,但你應該能夠得到這個想法。
對於不同的用例,您可能必須有一堆不同的輸入類型,所以從簡單和一般化開始就可以了。你也可以看看CRUD模塊的實現。
我的DisplayAttribute
類(字段的元數據)類似於以下內容。你可以用它作爲出發點。
public class DisplayAttribute {
public Boolean display = Boolean.TRUE;
public String type = "";
public String name;
public DisplayAttribute(String name){
this.name = name;
this.display = Boolean.TRUE;
}
public DisplayAttribute(String name, Boolean display){
this.name = name;
this.display = display;
}
... overridden equals and hash
}
編輯 如何領域得到呈現? 控制器將元數據(DisplayAttribute
)傳遞給視圖,在這種情況下,元數據僅包含字段的名稱以及是否可以顯示。
型號
這裏模型中包含的字段來渲染,但是你可以很容易地從數據庫中檢索這些。我的模型是通用的,因爲我意識到我一直在爲多個模型一遍又一遍地做同樣的事情。
我實現我自己的接口,它給我getFields
方法。我也維護兩張地圖,所以給定一個屬性,我可以得到它的DisplayAttribute,並給出一個DisplayAttribute,我得到它的名字。在需要時從視圖中調用此模型的方法。
public class GenericSimpleDBModel implements SimpleDBModel {
public static AmazonSimpleDB sdb = null;
private static final String bracketRemovalPattern = "(^.*?\\[|\\]\\s*$)";
private Map<DisplayAttribute, Set<String>> data = new TreeMap<DisplayAttribute, Set<String>>(new UuidComparator());
private Map<String, DisplayAttribute> attributeCache = new HashMap<String, DisplayAttribute>();
protected final String DOMAIN_NAME;
public GenericSimpleDBModel() {
initialize(getFields());
this.DOMAIN_NAME = "dishes";
}
protected void initialize(String[] fields) {
data = new TreeMap<DisplayAttribute, Set<String>>(new UuidComparator());
attributeCache = new HashMap<String, DisplayAttribute>();
for (String f : fields) {
// if (f.equals(getUUIDField()) || f.equals(getIntegrityField())) {
if (f.endsWith("uuid") || f.endsWith("integrity")) {
setValue(f, "", Boolean.FALSE);
} else {
setValue(f, "", Boolean.TRUE);
}
}
}
protected void initialize(Set<DisplayAttribute> fields) {
data = new TreeMap<DisplayAttribute, Set<String>>(new UuidComparator());
attributeCache = new HashMap<String, DisplayAttribute>();
for (DisplayAttribute atr : fields) {
setValue(atr.name, "");
}
}
... more methods
}
你的問題有點過於籠統,很難給你任何回答這樣的問題 –
@勒布Cesbron:它是否有助於如果它playframework沒有血緣關係,而是JSP/JAVA? – user1361914
這些表格後來在哪裏提交?我不知道如何處理提交動態表單,因爲你不知道你要存儲什麼。關於驗證,您可以使用jquery定義驗證器,並將這些類定義爲您的DynameForm的一部分。 – seePatCode