如何从动态表中向弹簧启动控制器发送对象列表,该表通过按钮单击将项目添加到表中
在我的Spring Boot项目中,用户必须输入其银行详细信息(选择银行名称,分支名称,输入帐号)。还有一个表,在用户单击“添加银行”后,将使用jQuery添加一行。这些表行还隐藏了一些输入类型。实际上,我必须将输入隐藏值从表发送到控制器,并将值保存在数据库中。我尝试使用列表来做到这一点,但无济于事。我有其他方法可以实现这一目标吗?以及如何将表行绑定到jQuery中的控制器。 是我的jQuery和控制器代码
$('#Addbank').click(function() {
alert($('#bank').children().length);
if ($('#banktable').children().length == 0)
$('#banktable').append("");
$('#banktable').append("<tr><td>" + $('#Bank option:selected').text() +"<input type='hidden' value="+$('#Bank').val() +">"+ "</td><td>" + $('#Branch option:selected').text() +
"</td><td>" + $('#accno').val() +"<input type='hidden' value="+ $('#accno').val()+ "</td><td><button type='button' class='btn' value='delete'>Delete</button></td></tr>");
})
的一部分
appl_banks = new BankDto();
for (int i = 0; i <= 50; i++) {
appl_banks.addBank(new Appl_bank());
System.out.println(appl_banks);
}
控制器
<table class="table table-hover" id="banktable">
<tr>
<th>Bank Name</th>
<th>Branch Name</th>
<th>Account Number</th>
</tr>
<tbody>
<tr th:each='b,item:${applbank}'>
<td><input type='hidden'
th:value='*{applbank[__${item.index}__].bankcd}'></td>
<td><input type='hidden'
th:value='*{applbank[__${item.index}__].branchcd}'></td>
<td><input type='hidden'
th:value='*{applbank[__${item.index}__].acno}'></td>
</tr>
</tbody>
</table>
是
以下 私人列表&lt; appl_bank&gt; appl_banks;
public BankDto() {
this.appl_banks=new ArrayList<>();
// TODO Auto-generated constructor stub
}
public BankDto(List<Appl_bank> appl_banks) {
super();
this.appl_banks = appl_banks;
}
public List<Appl_bank> getAppl_banks() {
return appl_banks;
}
public void setAppl_banks(List<Appl_bank> appl_banks) {
this.appl_banks = appl_banks;
}
public void addBank(Appl_bank appl_bank)
{
this.appl_banks.add(appl_bank);
}
}
银行详细信息表银行详细信息表应将其提交给控制器
In my spring boot project there is a part where the user has to enter his bank details(select bank name, branch name , enter account number). There is also a table where after the user clicks on "Add Bank" a row will be added to a table using jquery. These table rows also have some input type hidden on them. I actually have to send the input hidden values from the table to the controller and save the values in the database. I have tried using lists to do it but to no avail. Is there some other way I can achieve this. And also how do I bind the table rows to the Controller in Jquery. Below is my jquery and controller codes
$('#Addbank').click(function() {
alert($('#bank').children().length);
if ($('#banktable').children().length == 0)
$('#banktable').append("");
$('#banktable').append("<tr><td>" + $('#Bank option:selected').text() +"<input type='hidden' value="+$('#Bank').val() +">"+ "</td><td>" + $('#Branch option:selected').text() +
"</td><td>" + $('#accno').val() +"<input type='hidden' value="+ $('#accno').val()+ "</td><td><button type='button' class='btn' value='delete'>Delete</button></td></tr>");
})
The part of controller where I initialize a list before adding to a model
appl_banks = new BankDto();
for (int i = 0; i <= 50; i++) {
appl_banks.addBank(new Appl_bank());
System.out.println(appl_banks);
}
The table code in HTML
<table class="table table-hover" id="banktable">
<tr>
<th>Bank Name</th>
<th>Branch Name</th>
<th>Account Number</th>
</tr>
<tbody>
<tr th:each='b,item:${applbank}'>
<td><input type='hidden'
th:value='*{applbank[__${item.index}__].bankcd}'></td>
<td><input type='hidden'
th:value='*{applbank[__${item.index}__].branchcd}'></td>
<td><input type='hidden'
th:value='*{applbank[__${item.index}__].acno}'></td>
</tr>
</tbody>
</table>
My wrapper class
public class BankDto {
private List<Appl_bank> appl_banks;
public BankDto() {
this.appl_banks=new ArrayList<>();
// TODO Auto-generated constructor stub
}
public BankDto(List<Appl_bank> appl_banks) {
super();
this.appl_banks = appl_banks;
}
public List<Appl_bank> getAppl_banks() {
return appl_banks;
}
public void setAppl_banks(List<Appl_bank> appl_banks) {
this.appl_banks = appl_banks;
}
public void addBank(Appl_bank appl_bank)
{
this.appl_banks.add(appl_bank);
}
}
Bank details table The bank details table which should be submitted to the controller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经通过将隐藏的输入字段提供相同的名称并通过控制器中的数组访问它们来解决此问题。
jQuery part(插入具有相同名称的隐藏输入字段)
如何访问控制器中的输入字段名称
,希望帖子可以帮助遇到此错误的人:D
I had already managed to solve this by giving the hidden input fields the same name and accessing them as an array through in the Controller.
Jquery part(inserted hidden input fields with same name)
How to access input fields name in Controller
Hoping post helps someone who's stuck in this error :D