2016-08-02 50 views
1

的名单我开始绘制字符串的集合:Spring MVC的:GET复杂的对象

/abc?type=x,y,z 

,我绘制了一组枚举

@RequestParam(value = "type") Set<MyEnum> types 

现在我想补充另一参数分配给每种类型。例如像:

/abc?type=x:withProperty1,y:withProperty2,z:withProperty3 

,并且理想地映射到某个容器

@RequestParam(value = "type") Set<MyContainer> containers 

,然后列表:

class MyContainer { 
    MyEnum type 
    String property 
} 

是否有可能与Spring MVC?或者,我所能做的只是将其绑定到两个不同的列表:

/abc?type=x,y,z&properties=withProperty1,withProperty2,withProperty3 
+0

您能简单介绍一下您的问题? –

+0

我需要扩展现有的API并通过pojos列表 – piotrek

+0

嗨:)它对你有用。我搜索了你.http://www.studytrails.com/frameworks/spring/spring-mvc-controller-input。 jsp –

回答

0

如果你传递一个参数具有相同的名称将抵达你的控制器如之前被转换成一个数组:

网址...在你的控制器/somethig?type=x&type=y&type=z

你必须定义请求parametere作为数组

@requestMapping(value="/something",method=RequestMethod.GET) 
public void getTypes(@RequestParam(value = "type") String[] types){ 
    for(String type : types){ 
    System.out.print(type + "\t"); 
    } 
} 

因此你将有

x和yž

所以你的情况您可以使用此解决方案。

网址... /somethig?type=x&type=y&type=z&property=1&property=2&property=3

在控制器:

@requestMapping(value="/something",method=RequestMethod.GET) 
public void getTypes(@RequestParam(value = "type") String[] types,@RequestParam(value = "property") String[] properties){ 
    for(int i=0;i<types.length();i++){ 
    MyContainer mc=new MyContainer(); 
    mc.setType(types[i]); 
    mc.setProperty(properties[i]); 
    } 
} 
+0

里面的pojos列表是的,我只是希望我可以避免手动检查,如果两个列表具有相同的大小和建筑容器 – piotrek

+0

可以ü添加您的表格,以便我可以为您提供其他解决方案 –

+0

我没有表格。这是一个休息的终点 – piotrek