带有多个属性的绑定对象选择式java

发布于 2025-01-21 23:04:58 字数 1446 浏览 0 评论 0原文

我正在尝试从具有两个属性(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2025-01-28 23:04:59

答案是:

显示在视图中,然后返回控制器通过添加参数itemValue =“ id”来绑定到对象。

<form:select items="${authors}" multiple="true" path="author" itemValue="id" class="form-Control" />

the answer is this:

display List into view and then back to controller bind to object by adding parameter itemValue="id"

<form:select items="${authors}" multiple="true" path="author" itemValue="id" class="form-Control" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文