简体   繁体   English

从列表jsp中获取id值

[英]Get id value from list jsp

I have view on my jsp like this:我对我的jsp有这样的看法:

<form:form name = "command" 
         method = "post" 
         action = "withdrawRequest" 
          class = "form-horizontal group-border-dashed" 
          style = "border-radius: 0px;">
<table class = "table table-bordered" 
          id = "datatable-icons" > <!-- start table table-bordered -->
    <thead>
        <tr>
            <th>ID</th>
            <th>NO</th>
            <th>AMOUNT (RP)</th>
            <th>DOCTOR BALANCE (RP)</th>
            <th>DOCTOR ID</th>
            <th>DOCTOR NAME</th>
            <th>TRANSFER ADMIN NAME</th>
            <th>TRANSFER DATE</th>
            <th>TRANSFER REFERENCE</th>
            <th>TRANSFERRED</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var = "withdrawal" 
                 items = "${listWithdrawals}" 
             varStatus = "loopStatus">
        <tr class="odd gradeX">
            <td>${withdrawal.id}</td>
            <td>${loopStatus.index + 1}</td>
            <td class="text-right"><fmt:formatNumber type="number" maxFractionDigits="2" value="${withdrawal.amount}" /></td>
            <td class="text-right"><fmt:formatNumber type="number" maxFractionDigits="2" value="${withdrawal.doctor_balance}" /></td>
            <td>${withdrawal.doctor_id}</td>
            <td>${withdrawal.doctor_name}</td>
            <td>${withdrawal.transfer_admin_name}</td>
            <td><%-- <fmt:formatDate type="date" value="${withdrawal.transfer_date}" /> --%>${withdrawal.transfer_date}</td>
            <td>${withdrawal.transfer_reference}</td>
            <td class="text-center">
                <c:if test="${withdrawal.transferred == true}">
                    <span class="label label-success">Transferred</span>
                </c:if>
                <c:if test="${withdrawal.transferred == false}">
                    <button class="btn btn-warning btn-sm md-trigger" data-modal="form-primary" type="button">Pending</button>
                </c:if>
                <div class="md-modal colored-header danger custom-width md-effect-9" id="form-primary">
                    <div class="md-content">
                        <div class="modal-header">
                            <h3>Confirm Withdrawal Request</h3>
                            <button type="button" class="close md-close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        </div>
                        <div class="modal-body form">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">Admin Name</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="adminName" value="${adminName}">
                                    <input type="text" class="form-control" name="id" value="${withdrawal.id}" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">Transfer Reference</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="transferReference" value="${transferReference}">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default btn-flat md-close" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-danger btn-flat md-close" name="submitRequest">Submit</button>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        </c:forEach>
    </tbody>
</table>
</form:form>

I cannot get the ID of which button I selected from view,我无法从视图中获取我选择的按钮的 ID,

<input type = "text" 
      class = "form-control" 
       name = "id" 
      value = "${withdrawal.id}" />

If I use this code如果我使用此代码

String withdrawalId = request.getParameter("id");

The value is always 120. For example:该值始终为 120。例如:

在此处输入图片说明

How do I get the correct ID when I click the modal window?单击模态窗口时如何获取正确的 ID?

It seems that the problem is in Nifty Modal Window Effects you are using for popup.问题似乎出在您用于弹出窗口的 Nifty Modal Window Effects中。 It opens modal window for the last row.它打开最后一行的模式窗口。
It can be fixed with following jsp modifications.它可以通过以下jsp修改来修复。

  1. Move modal window declaration out of table.将模态窗口声明移出表格。 Make it single for page.使其成为单页。 And rename id field to make it unique, eg transferredId并重命名 id 字段以使其唯一,例如transferredId
  2. Add onclick to buttons opening modal window in order to init it (set transferredId and others) with necessary valuesonclick添加到打开模态窗口的按钮中,以便使用必要的值对其进行初始化(设置 transferId 和其他)

So your form would be (just ending to save space)所以你的表格将是(只是结束以节省空间)

                ....
                <td class="text-center">
                ....
                <c:if test="${withdrawal.transferred == false}">
                    <button class="btn btn-warning btn-sm md-trigger" data-modal="form-primary" type="button"
                    onclick="document.getElementById('transferredId').value=${withdrawal.id}">
                    Pending
                    </button>
                </c:if>
            </td>
        </tr>
        </c:forEach>
    </tbody>
</table>
<div class="md-modal colored-header danger custom-width md-effect-9" id="form-primary">
    <div class="md-content">
        <div class="modal-header">
            <h3>Confirm Withdrawal Request</h3>
            <button type="button" class="close md-close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body form">
            <div class="form-group">
                <label class="col-sm-3 control-label">Admin Name</label>
                <div class="col-sm-9">
                    <input type="text" class="form-control" name="adminName" value="${adminName}">
                    <input type="text" class="form-control" name="transferredId" id="transferredId" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">Transfer Reference</label>
                <div class="col-sm-9">
                    <input type="text" class="form-control" name="transferReference">
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default btn-flat md-close" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-danger btn-flat md-close" name="submitRequest">Submit</button>
        </div>
    </div>
</div>

After that String withdrawalId = request.getParameter("transferredId");在该String withdrawalId = request.getParameter("transferredId");之后, String withdrawalId = request.getParameter("transferredId"); will return correct id.将返回正确的 ID。

Check the generated HTML.检查生成的 HTML。 Looks to me that it contains listWithdrawals.length instances of that <input and all of them have the same name.在我看来,它包含该<input listWithdrawals.length实例,并且它们都具有相同的名称。

request.getParameter("id") is going to return a single value, not a list of all the values. request.getParameter("id")将返回单个值,而不是所有值的列表。 What you need is request.getParamaterValues("id") that is going to return all of them.您需要的是request.getParamaterValues("id")将返回所有这些。

This is because when the form is submitted all those input values are going to be sent.这是因为当提交表单时,所有这些输入值都将被发送。

So the simple solution (with no JavaScript magic) is to get all the lists of values with request.getParamaterValues and deal with everything that changed.所以简单的解决方案(没有 JavaScript 魔法)是使用request.getParamaterValues获取所有值列表并处理所有更改的内容。 ids[0] and transferReferences[0] would be the needed values for the first item in listWithdrawals : ids[0]transferReferences[0]将是listWithdrawals第一项所需的值:

String[] ids = request.getParamaterValues("id");
String[] transferReferences = request.getParamaterValues("transferReference");

for (int i = 0; i < ids.length; i++) {
    System.out.println(ids[i]);
    System.out.println(transferReferences[i]);
}

Another solution would be to generate unique names for each input/item in listWithdrawals :另一种解决方案是为listWithdrawals每个输入/项目生成唯一名称:

<c:forEach var="withdrawal" items="${listWithdrawals}" varStatus="loopStatus">
    <input type="text" class="form-control" name="${'transferReference_'.concat(withdrawal.id)}" value="${withdrawal.id}" />
</c:forEach>

And then you'd be able to access the transferReference value of a withdrawal with ID 119 like this:然后您就可以像这样访问 ID 为 119 的withdrawaltransferReference值:

request.getParameter("transferReference_119")

请原谅我没有将其添加为评论,因为我不能,因为我仍然没有 50 声望(仍然是 16),我只是想让您测试一些东西,尝试刷新页面,然后打开 ID 为 119 的行,例如首先时间,我的意思是任何 Id 而不是 120,看看会发生什么,如果您第一次打开的值仍然存在,那么问题不在 id 120 中,问题是您可以使用 if not null 来设置值一些地方,所以第一次它将为空并将值添加到它,但下次它已经有一个值所以你不能改变它,如果你这样做后你不能解决问题,请上传您的控制器,以便我们检测问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM