2014-09-02 28 views
0

我想在我的視圖中顯示字符串列表作爲可重複的輸入文本控件。這裏是我的模型:Play框架2.3.2:如何在scala模板中渲染列表或地圖

public class User { 
    @Required 
    public String email; 
    public String password; 
    public List<String> products; 
} 

控制器:

public static Result index() { 
    Form<User> userForm = Form.form(User.class); 

    Map<String,String> anyData = new HashMap<String,String>(); 
    List<String> listProduct = new ArrayList<String>(); 
    listProduct.add("p1"); 
    listProduct.add("p2"); 
    userForm = userForm.fill(new User("[email protected]", "secret", listProduct)); 
    return ok(views.html.index.render(userForm)); 
} 

查看:

@(userForm: Form[models.User]) 
@import helper._ 
@import models.User 
@import scala._ 

@main("Welcome to Play") { 

<form id="formUser" action="/user/apply" method="post"> 
    @inputText(userForm("email")) 
    @inputText(userForm("password")) 
    @for(product <- userForm("products")) { 
     <input type="text" name="@product" value="@product"> 
    } 

    <input type="submit" value="submit"/> 
</form> 
} 

錯誤是:

value map is not a member of play.data.Form.Field 

我也試過形式幫手@Repeat。但它只是不工作。

@repeat(userForm("products"), min = 0) { 
     product => @inputText(product) 
    } 

錯誤:

not found: value product 

我使用的播放2.3.2在Java中。 任何想法是什麼問題?

蘇拉傑

回答

1

你只需要記住,視圖模板被解析到Scala的功能和代碼逃脫@字符。你的第二個解決方案很好。在這種情況下,你只需要以適當的方式來設置代碼的格式,它就像一個魅力。

@repeat(userForm("products"), min = 0) { product => 
    @inputText(product) 
} 
+0

謝謝。我格式化的代碼,當你悲傷時,它就像魅力一樣。我還記下了你對逃避角色的評論並理解了這個問題。 – Suraj 2014-09-07 13:15:00

+0

@Suraj我不明白關於逃跑角色的問題 - 有人能啓發我嗎?另外,是否可以使用at-for,或者只能使用at-repeat? – bharal 2017-01-03 01:05:06