如何解决如何给<form:input path =“ {java.util.set}”>设置modelAttribute值?
我有一个具有以下结构的模型:
class Person {
private int id;
private Set<Person> friends = new Set<Person>();
}
当我在<form:form>
中为此类使用modelAttribute时,如何设置friends属性的值?
请帮助!
解决方法
最初在Model.java文件中,添加ID字段和好友字段的setter和getter。
Model.java
public class Person {
private int id;
Set<String> friends= new HashSet<String>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<String> getFriends() {
return friends;
}
public void setFriends(Set<String> friends) {
this.friends = friends;
}
}
然后在PersonController类代码中应如下所示:
PersonController
@RequestMapping(value = "/savePerson",method = RequestMethod.POST)
public String savePerson(@ModelAttribute("person") Person person){
Person tempPerson = new Person();
Set<String> frnd = new HashSet<String>();
tempPerson.setId(person.getId());
/* This code line depends on how you are planning to get the friends list from form and then add it to 'frnd' set by frnd.add() method and pass the set to following setter*/
tempPerson.setFriends(frnd);
personService.savePerson(tempPerson);
return "redirect:/persons";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。