从列表jsp获取id值

时间:2016-04-19 10:51:41

标签: java jsp httprequest

我对我的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>

我无法获取从视图中选择的按钮的ID,

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

如果我使用此代码

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

值始终为120。 例如:

enter image description here

单击模态窗口时如何获得正确的ID?

3 个答案:

答案 0 :(得分:1)

问题出现在用于弹出窗口的 Nifty Modal Window Effects 中。它打开最后一行的模态窗口。
它可以通过以下jsp修改来修复。

  1. 将模态窗口声明移出表格。将它设为单页。并重命名id字段以使其唯一,例如transferredId
  2. onclick添加到打开模态窗口的按钮,以便使用必要的值初始化它(设置transferredId和其他)
  3. 所以你的表格会(为了节省空间而结束)

                    ....
                    <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>
    

    之后String withdrawalId = request.getParameter("transferredId");将返回正确的ID。

答案 1 :(得分:0)

检查生成的HTML。在我看来,它包含listWithdrawals.length<input个实例,并且所有实例都具有相同的名称。

request.getParameter("id")将返回单个值,而不是所有值的列表。你需要的是request.getParamaterValues("id"),它将返回所有这些。

这是因为当提交表单 all 时,将发送这些输入值。

因此,简单的解决方案(没有JavaScript魔法)是使用request.getParamaterValues获取所有值列表并处理所有更改的内容。 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]);
}

另一个解决方案将为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>

然后您就可以访问ID为119的transferReference的{​​{1}}值,如下所示:

withdrawal

答案 2 :(得分:0)

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