带有多个属性的绑定对象选择式java
我正在尝试从具有两个属性(firstName,lastName)的对象作者中显示“选择表”。无法将两个属性添加到ItemLabel。当我尝试从列表中显示这些诱因时,没有绑定,因为数据库未使用此属性进行更新。如何将多个属性放在绑定中选择表单?
Author <form:select itemValue="id" itemLabel="firstName" path="author"
items="${authors}" />
@Controller
public class ArticleController {
@Autowired
public ArticleDao articleDao;
@Autowired
public AuthorDao authorDao;
@RequestMapping("list-articles")
public String allArticles(Model model){
model.addAttribute("article", articleDao.findAllArticle());
return "articleList";
}
@RequestMapping("add-article")
public String addArticle(Model model){
model.addAttribute("article", new Article());
return "add-article";
}
@RequestMapping(value = "add-article", method = RequestMethod.POST)
public String saveArticle(@ModelAttribute("article") Article article, BindingResult bindingResult){
articleDao.saveArticle(article);
return "redirect:/list-articles";
}
@ModelAttribute("authors")
public List<Author> authors() {
return this.authorDao.findAllAuthors();
}
}
@Entity
public class Article {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 200)
private String title;
@ManyToOne
private Author author;
//setters & getters
I am trying to display select form from object Author with two attributes (firstName, lastName). Cannot add two attributes to itemLabel. When I tried to display those attrubutes from list then there is no binding as database is not updated with this attributes. How can I put multiple attributes to select form with binding?
Author <form:select itemValue="id" itemLabel="firstName" path="author"
items="${authors}" />
@Controller
public class ArticleController {
@Autowired
public ArticleDao articleDao;
@Autowired
public AuthorDao authorDao;
@RequestMapping("list-articles")
public String allArticles(Model model){
model.addAttribute("article", articleDao.findAllArticle());
return "articleList";
}
@RequestMapping("add-article")
public String addArticle(Model model){
model.addAttribute("article", new Article());
return "add-article";
}
@RequestMapping(value = "add-article", method = RequestMethod.POST)
public String saveArticle(@ModelAttribute("article") Article article, BindingResult bindingResult){
articleDao.saveArticle(article);
return "redirect:/list-articles";
}
@ModelAttribute("authors")
public List<Author> authors() {
return this.authorDao.findAllAuthors();
}
}
@Entity
public class Article {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 200)
private String title;
@ManyToOne
private Author author;
//setters & getters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是:
显示在视图中,然后返回控制器通过添加参数itemValue =“ id”来绑定到对象。
the answer is this:
display List into view and then back to controller bind to object by adding parameter itemValue="id"