4
回答
playframework中form提交多个对象
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

如下图:通常我们在struts2中实现这个效果,并且取得值是很方便的。在前台jsp页面中定义<input type="text" class="input01" maxlength="30"  name="userPhoneVo["+phoneSeq+"].phoneNumber" />

在action中定义一个   

private List<UserPhoneVo> userPhoneVo = new ArrayList<UserPhoneVo>();

这样就可以取得每一组中各个字段的值了。   

 

我现在的问题是在Play Framework中如何才能在控制器中方便的得到每一个组中各个字段的值呢?

举报
wenivan
发帖于7年前 4回/1K+阅
共有4个答案 最后回答: 7年前

Play 可支持数组参数,例如

public static void show(List id) {
    for(String anId : id) {
        System.out.println(id); 
    }
}

文档里没有提到像Struts这么强的参数映射方法。

	public static void saveProfile(User userVo, List<String> mailType,
			List<String> mailAddress, List<UserPhone> UserPhoneVO,
			List<String> UserPhone_phoneNumber,
			List<String> UserPhone_selfPhoneTypeName,
			List<String> UserPhone_cityCode,
			List<String> UserPhone_subCode,
			List<String> UserPhone_countryCode,
			List<String> UserPhone_phoneTypeName) {

我尝试过这样单独去定义数组参数,唯一的缺点就是代码行数太多了,这些数组参数分组写入的时候不好取。

红薯,请问play它可以实现JPA的数组参数吗?

public static void saveProfile(User userVo, List<String> mailType,  
 List<String> mailAddress, List<UserPhone> UserPhoneVO,){


} 
我的理想做法是这样的。不知道能不能实现?或者请告诉实现途径。谢谢
What do you use to submit multiple records? A typical use case is that an order creation form will have several order items data input by the end user.  My approach is as follows:
At the client side: Put an hidden field in the <form> like <input type='hidden' name='items'/>. User add order items either through multiple predefined input fields or dynamically generated fields using javascript. Hook a event handler to form.submit event, construct a string with all order items information using special separator e.g. ';', update the hidden items field with the calculated string val. Then proceed with the submission process.
At the server side, parse the item string and construct order items object on the fly, add them into a new order model and save it.
Does anyone has better approach with Play!
replay:
Play support multiple values that you can then bind as Array or List.
But anyway it is always difficult to map complex data structure (with
repeat) into URL form encoded representation.

Perhaps you could do it using Ajax and JSON?
顶部