如何在JSP页面上映射一对多关系并在提交页面时存储它们? [英] How do I map one to many relationship on a JSP page and store them while submitting the page?

查看:141
本文介绍了如何在JSP页面上映射一对多关系并在提交页面时存储它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类Student,另一类是Books. Student类与Books具有一对多的关系.

Suppose, I have one class Student and another one is Books. The Student class having one to many relationship with Books.

现在,我想从用户名(如学生姓名,城市等)中获取数据,并在同一页面上添加诸如书名,作者,价格等之类的Books详细信息.用户可以添加多个Books详细信息(显然).然后,我想提交包含所有嵌套对象(书)数据以及父对象的页面.我该怎么办?

Now, I want to take the the data from user like student name, city etc and add the Books details like book name, author, price etc. on the same page. User can add multiple Books details(obviously). And then, I want to submit the page with all the nested object(books) data filled along with parent. How can I do that?

只要满足要求,您可以采用任何其他示例代替学生和书籍.

You can take any other example instead of student and books as long as the requirement is satisfied.

这是我的JSP文件:

// here student is a parent object and book is a child object having name,author etc details.

<body>
  ${company} form
  <form:form commandName="student" action="../homeController/getDetails"
      id="studentForm">
    Name: 
    <form:input path="name" id="name" />
    City:
    <form:input path="city" id="city" />
    <form:input path="books.bookName" id="bookname"/>
    <form:input path="books.bookAuthor" id="bookauthor"/>
    <button type="submit" name="submit" style="width: 50px; height: 20px;"
        value="Submit">Submit</button>
  </form:form>
</body>

如果您可以为我提供一个很好的链接,那也将是一个很大的帮助.

If you can provide me a good link, that would also be a great help.

推荐答案

您必须为此编写JavaScript,但我会给您一个想法.

You have to write javascript for it but I will give you the idea to do so.

在您的JSP中有一个表格,可以说name=StudentDataForm.在这种形式下,您可以执行以下操作-

In your JSP have a form lets say name=StudentDataForm. In that form you do something like -

<form name="StudentDataForm" action="/submitStudentData.htm" method="post">
<div id="student1">
Name : <input type="text" id="studentname1" name="studentname" >
City : <input type="text" id="studentcity1" name="city" >
<div id="books>
Books : <input type="text" id="student1book1" name="bookstudent1" >
Books : <input type="text" id="student1book2" name="bookstudent1" >
</div>
</div>
<div id="student2">
Name : <input type="text" id="studentname2" name="studentname" >
City : <input type="text" id="studentcity2" name="city" >
<div id="books>
Books : <input type="text" id="student2book1" name="bookstudent2" >
Books : <input type="text" id="student2book2" name="bookstudent2" >
Books : <input type="text" id="student2book3" name="bookstudent2" >
Books : <input type="text" id="student2book4" name="bookstudent2" >
</div>
</div>
</>
</form>

对于相同实体,name的概念保持相同,就像学生的所有书籍都有name = bookstudent2一样.然后,在您的控制器或servlet上,您可以将数据获取为

Concept is keep the name same for same entities like all books for a student have name = bookstudent2. Then on your controller or servlet you can get the data as

String[] books = request.getParameterValues("bookstudent2");

根据正在做的事情,您只能将command object显示在UI上(通过使用正确的路径在命令对象中的Student或Books中进行迭代).但是,您无法以其他方式做到这一点,这意味着您无法在UI上操作数据并无法通过命令Object发送数据.

By what you are doing getting the command object you can only show it on the UI (By iterating on the Student or Books in the command object using their correct path). But you cannot do it the other way around meaning you cannot manipulate data on UI and send it via command Object.

如何在不使用JavaScript的情况下做到这一点.提交表单时如何将子对象的数据与父对象绑定?

How can i do that without using javascript. How can i bind child object's data with the parent object while submitting the form?

如果您有固定数量的学生和固定数量的书,您可以做自己想做的事情.请查看此链接.但是,如果要根据我的理解范围动态添加学生"和书本",则必须使用上述方法.

If you have fixed number of student and fixed number of books you can do what you desire. take a look at this link. But if you want to add Student and Books dynamically as far as my understanding goes you have to use the way I have described above.

这篇关于如何在JSP页面上映射一对多关系并在提交页面时存储它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆