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类型的。
原文地址:http://blog.youkuaiyun.com/asarja/article/details/8978286