您的位置:首页 > 编程语言 > Java开发

springMVC 参数传递之:数组,Map,List,Set以及自定义复杂类型参数

2014-04-04 17:01 711 查看
Map,List,Set都无法直接作为Controller的参数类型,而必须自定义一个类,类中有一个Map,List,Set的类型,将数据传递给自定对象的Map,Set,List。下面就是一个Map类型参数的例子,Set,List的传参方式与之相似,只要将Map类型改为List或Set类型即可

jsp:

<form action="/hello/getMap" method="post">

<input name="mapVo['a'].name">

<input name="mapVo['a'].password" type="password">

<input name="mapVo['b'].name">

<input name="mapVo['b'].password" type="password">

<input type="submit" value="submit">

</form>

java:

@RequestMapping(value="/getMap",method=RequestMethod.POST)

public void getMap(MapVo mapVo){

Set set = mapVo.getMapVo().keySet();

Iterator iterator = set.iterator();

while(iterator.hasNext()){

Object name = iterator.next();

PersonVo p = mapVo.getMapVo().get(name);

System.out.print(name+" "+p.getName()+" "+p.getPassword());

System.out.println();

}

}

public class MapVo {

private Map<String,PersonVo> mapVo;

public Map<String, PersonVo> getMapVo() {

return mapVo;

}

public void setMapVo(Map<String, PersonVo> mapVo) {

this.mapVo = mapVo;

}

}

public class PersonVo {

private String name;

private String password;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

通过以上代码,springMVC可以自动用map接受前台的数据,key是String类型的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐