如何在spring mvc 3中为Model属性绑定List

时间:2011-08-05 20:55:01

标签: java list spring-mvc bind modelattribute

User.java

public class User{
       private String name;
       private List<Link> links;
}

Link.java

public class Link{
       private String addr;
       private String tel;

}

UserController.java

@RequestMapping(value = "user", method = RequestMethod.GET)
public String test(Model model) {
        model.addAttribute("user",new User());
        return "user";

}

@RequestMapping(value = "user", method = RequestMethod.POST)
public String test(User user) {
        System.out.println(user.getName());
        List<Link> links = user.getLinks();
        for(Link link:links){
            System.out.println(link.getAddr());
            System.out.println(link.getTel());
        }

        return "user";
}

user.jsp

[form:form modelAttribute="user" method="post" action="/user"]
[form:input path="name" /]
[form:input path="addr" /]
[form:input path="tel" /]
[form:input path="addr" /]
[form:input path="tel" /]
[input type="submit"][/input]
[/form:form]

谁能告诉我怎么做!

1 个答案:

答案 0 :(得分:0)

[form:form modelAttribute="user" method="post" action="/user"]
[form:input path="name" /]
[form:input path="links[0].addr" /]
[form:input path="links[0].tel" /]
[form:input path="links[1].addr" /]
[form:input path="links[1].tel" /]
[input type="submit"][/input]
[/form:form]