lundi 30 mars 2015

checkbox and respective row's values from jsp to java controller file

I am working in spring mvc, now focusing on insert operation to a table from jsp followed by executing controller.


I am having a list of values from search query in database. I just want to save few of the rows and not all of them. For this I put a checkbox to select which rows need to be deleted, so that it can only inserted in table.


If I am choosing 3rd row in that table, I am getting the chackbox value only as good as selected. But I cant get other values in that 3rd row, instead of that I am getting only 1st row values. Can anyone give a right way to do this? enter image description here JSP:



<div id="login_block">
<center>
<span style="font-size: 2em;">Funder Assign </span></center>
<div style="margin-top: 30px;">
<form id="saveAssign" method="POST" name="saveAssign"
action="saveAssign.htm" autocomplete="on">

<label class="bold_text" style="margin-right:5%; margin-left:10%;">Loan Type</label>
<select name="producttype" id="producttype" style="width:29%;">
<option value="0">--Select--</option>
<c:forEach var="entry" items="${productList}">
<option value="${entry.key}">${entry.value}</option>
</c:forEach>
</select><br/><br/>
<table border=1>
<thead>
<tr>
<th>Select</th>
<th>Loan A/c No</th>
<th style="width;1%;">Type</th>
<th>Funder</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<c:forEach var="loan" items="${loans}">
<tr>
<td><input type="checkbox" name="loanId" value="${loan.id}"></td>
<td><input type="text" name = "loanAccNo" value="${loan.loanAccNo}" readonly="readonly"></td>
<td><input type="text" name = "product" value="${loan.product.name}" readonly="readonly" ></td>
<td><select id="funderId" name="funderId" >
<option value="0">--Select--</option>
<c:forEach var="fund" items="${fundMap}">
<option value="${fund.key}">${fund.value}</option>
</c:forEach>
</select></td>
<td><select id="purpose" name="purpose" style="width:75%;">
<option value="0">--Select--</option>
<c:forEach var="purpose" items="${fundProdPurpose}">
<option value="${purpose.id}">${purpose.id}</option>
</c:forEach>
</select></td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<input class="login_block_login_btn" type="submit" name="submit" style="margin-left:0%;"
value="Save" id="addfundsbtn" />
</div><br/><br/><br/>


</form>
</div>
</div>


Controller



public class SaveFunderAssignController extends AbstractController {

@Resource
private UserService userService;

@Resource
private CommonRepository commonRepository;


public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

String[] loanIds = request.getParameterValues("loanId");

if (!loanIds.equals("")) {
return save(request, response);
}
return cancel(request, response);
}

private ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
String[] loanIds = request.getParameterValues("loanId");
String[] loanAccNos = request.getParameterValues("loanAccNo");
String[] products = request.getParameterValues("product");
String[] funderIds = request.getParameterValues("funderId");
String[] purposes = request.getParameterValues("purpose");

LoanFinance loanFinance = new LoanFinance();
User user = userService.getLoggedInUser(request);

for (int i = 0; i < loanIds.length; i++) {
Long bankId = Long.parseLong(funderIds[i]);
Long loanId = Long.parseLong(loanIds[i]);
Long purpose = Long.parseLong(purposes[i]);
loanFinance.setFundingBankId(bankId);
loanFinance.setIdFundsProductPurpose(purpose);
loanFinance.setLoanId(loanId);
loanFinance.setLastModifiedBy(user.getId());
loanFinance.setIdEntityloan(loanId);
loanFinance.setAmount(10000.00);
loanFinance.setStatus("A");

commonRepository.save(loanFinance);
}

return doMapping("result", "Funder details added successfully",
request);
} catch (Exception e) {
e.printStackTrace();
return doMapping("result", "Unable to add funder details", request);
}
}




Aucun commentaire:

Enregistrer un commentaire