Reputation: 1078
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?
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 (String loanId : loanIds) {
int loanIdInt = Integer.parseInt(loanId) - 1;
Long bankId = Long.parseLong(funderIds[loanIdInt]);
Long loanIdLong = (long) loanIdInt + 1;
Long purpose = Long.parseLong(purposes[loanIdInt]);
System.out.println("loanIdInt**"+loanIdInt );
System.out.println("bankId**"+bankId );
System.out.println("loanIdLong**"+loanIdLong );
System.out.println("purpose**"+purpose );
}
return doMapping("result", "Funder details added successfully",
request);
} catch (Exception e) {
e.printStackTrace();
return doMapping("result", "Unable to add funder details", request);
}
}
After changed as your updated loop, Here loan Id only prints as same as selected value, but for other things are fetched from the 1st row...
Now loanId is prints in console...
I tried this following way to get the position of checked boxes, but here also loanId only as same, others are from 1st row.
I am looking for, if 1st checkbox is selected then loanId would be 1, if third checkbox and 4th checkbox selected then it would be 3,4.. Any help would be appreciated..
for (String string : loanIds) {
for (int i = 0; i < loanIds.length; i++) {
if (loanIds[i] == string) {
Integer ge = i;
System.out.println("I Value**"+i );
System.out.println("loanAccNos**"+loanAccNos[ge] );
System.out.println("funderIds**"+funderIds[ge] );
System.out.println("purposes**"+purposes[ge] );
System.out.println("loanIds**"+loanIds[ge] );
}
}
}
Upvotes: 0
Views: 2208
Reputation: 899
Problem is in your retrieval logic. You are retrieving all other values from int i = 0;
that's why it is showing first value. Try out below given loop.
for (String loanId : loanIds) {
int loanIdInt = Integer.parseInt(loanId) - 1;
Long bankId = Long.parseLong(funderIds[loanIdInt]);
Long loanIdLong = (long) loanIdInt + 1;
Long purpose = Long.parseLong(purposes[loanIdInt]);
loanFinance.setFundingBankId(bankId);
loanFinance.setIdFundsProductPurpose(purpose);
loanFinance.setLoanId(loanIdLong);
loanFinance.setLastModifiedBy(user.getId());
loanFinance.setIdEntityloan(loanIdLong);
loanFinance.setAmount(10000.00);
loanFinance.setStatus("A");
commonRepository.save(loanFinance);
}
Upvotes: 1