在Play中处理字段集!框架2.3(Java) [英] Handling fieldsets in Play! Framework 2.3 (Java)
问题描述
< div class =controlsid =orderlines-container>
< div class =orderline form-inline>
<选项禁用选择>选择产品...< / option>
@for(product< -Product.findAll()){
< option value =@ product.id> @ product.name< / option>
}
< / select>
< input class =form-controltype =numbername =quantity []/>
< select class =form-controlname =price []>
< option value =memberPrice>会员价格< / option>
< option value =publicPrice>公开价格< / option>
< / select>
< a href =#class =btn btn-lg btn-remove> < span class =glyphicon glyphicon-remove> < /跨度> < / A>
< / div>
< a href =#class =btn btn-success btn-smid =btn-add-create>添加行...< / a>
< / div>
我使用括号表示法,因为用户可以通过单击来添加尽可能多的命令行添加行...按钮。当点击按钮时,JavaScript函数会克隆< div class =orderline>
并将其追加到下方。所以基本上,提交的表单包含了几条命令行。
现在我想要做的是收集控制器中的所有数据,但我无法弄清楚去做吧。有人建议我在控制器中做这样的事情:
public static Result createOrder(String [] quantity,String [] (int i = 0; i< quantity.length; i ++){
...
}
for(int i = 0) ; i< productId.length; i ++){
...
}
for(int i = 0; i< price.length; i ++){
...
但问题在于它需要我执行对于 Array [String]
类型的自定义隐式 QueryStringBindable
。对于这样一个基本特征,这不算太多吗?
所以我的问题是我该如何处理具有相同参数的可变数量的表单姓名?像,你将如何实现一个购物车,你创建几个orderlines(以完全随机的顺序),然后提交它们来创建一个订单?
< PS:我已经尝试过不同的名称(例如[
productId0
, quantity0
,价格0
],然后[ productId1
, quantity1
, price1
]等等......),但是最终结果太复杂了,不能保证索引的准确性... 我在这里找到了答案: https://stackoverflow.com/ a / 20584282/2523628
解决方案是使用
地图<字符串,字符串[] GT; params = request()。body()。asFormUrlEncoded();
所有值存储在 Map
看起来像这样:
userId
- 1
orderDate
- 2014-09- 10T14:21
productId []
- 21
- 61
数量[]
- 1
- 2
价格[]
- memberPrice
- memberPrice
status
- 已处理
然后只需手动分析值。它看起来像在每个数组中存储的值相同,所以我仍然可以知道哪些匹配(这只是一个猜测)。
另外,括号表示法是无用的。如果删除,数据仍然是相同的,以相同的顺序。
我仍然想知道是否有其他解决方案,特别是使用自动绑定功能的解决方案。
I'm trying to figure out how Play! handles forms where multiple fields with the same name are used.
For example, take a look at this form snippet:
<div class="controls" id="orderlines-container">
<div class="orderline form-inline">
<select class="form-control" name="productId[]">
<option disabled selected>Select product...</option>
@for(product<-Product.findAll()) {
<option value="@product.id">@product.name</option>
}
</select>
<input class="form-control" type="number" name="quantity[]" />
<select class="form-control" name="price[]">
<option value="memberPrice" >Member price</option>
<option value="publicPrice" >Public price</option>
</select>
<a href="#" class="btn btn-lg btn-remove"> <span class="glyphicon glyphicon-remove"> </span> </a>
</div>
<a href="#" class="btn btn-success btn-sm" id="btn-add-create"> Add line...</a>
</div>
I'm using the bracket notation because the user can add as many orderlines as he would like by clicking the "Add line..." button. When clicking on the button, a JavaScript function clones the <div class="orderline">
and appends it just below. So basically, the submitted form contains several orderlines.
Now what I want to do is to collect all of the data in my controller, but I couldn't figure out how to do it. Some people suggested me to do something like this in the controller:
public static Result createOrder(String[] quantity, String[] productId, String[] price) {
for(int i=0; i<quantity.length; i++){
...
}
for(int i=0; i<productId.length; i++){
...
}
for(int i=0; i<price.length; i++){
...
}
}
But the problem with this is that it requires me to implement a custom implicit QueryStringBindable
for the type Array[String]
. Isn't that too much for such a basic feature?
So my question is how can I handle a form with a variable number of parameters that have the same name? Like, how would you implement a shopping cart where you create several orderlines(in a completely random order), and then submit them to create an order?
PS : I already tried to have different names (like [productId0
, quantity0
, price0
] for the first line, then [productId1
, quantity1
, price1
] and so on...) for each input, using JavaScript, but it ended up being too complex to keep the index accurate...
I have found an answer here: https://stackoverflow.com/a/20584282/2523628
The solution is to get the raw data using
Map<String,String[]> params = request().body().asFormUrlEncoded();
All the values are stored in a Map
that looks like this:
userId
- 1
orderDate
- 2014-09-10T14:21
productId[]
- 21
- 61
quantity[]
- 1
- 2
price[]
- memberPrice
- memberPrice
status
- processed
And then just manually parse the values. It looks like the values are stored in the same order in each array, so i'ts still possible to know which ones match (this is just a guess).
Also, the bracket notation is useless. If removed, the data is still the same, in the same order.
I'm still wondering if there is any other solution, especially one using the automatic binding feature.
这篇关于在Play中处理字段集!框架2.3(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!