复选框和相应行的值从 jsp 到 java 控制器文件
checkbox and respective row's values from jsp to java controller file
我在 spring mvc 工作,现在专注于从 jsp 到 table 的插入操作,然后执行控制器。
我有一个来自数据库中搜索查询的值列表。我只想保存几行而不是全部。为此,我在 select 上勾选了需要删除的行,这样它就只能插入到 table 中。
如果我选择 table 中的第 3 行,我得到的复选框值仅与 selected 一样好。但是我无法在第三行获得其他值,而不是我只获得第一行值。任何人都可以提供正确的方法吗?
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>
控制器
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);
}
}
随着你的更新循环改变后,这里的贷款 Id 只打印与 selected 值相同,但对于其他内容,从第一行获取...
现在 loanId 打印在控制台中...
我尝试了下面的方法来获取复选框的位置,但这里的 loanId 也只是一样,其他都是从第一行开始的。
我正在寻找,如果第一个复选框被 selected 那么 loanId 将是 1,如果第三个复选框和第四个复选框被 selected 那么它将是 3,4..任何帮助都会不胜感激..
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] );
}
}
}
您的检索逻辑有问题。您正在从 int i = 0;
中检索所有其他值,这就是它显示第一个值的原因。尝试下面给定的循环。
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);
}
我在 spring mvc 工作,现在专注于从 jsp 到 table 的插入操作,然后执行控制器。
我有一个来自数据库中搜索查询的值列表。我只想保存几行而不是全部。为此,我在 select 上勾选了需要删除的行,这样它就只能插入到 table 中。
如果我选择 table 中的第 3 行,我得到的复选框值仅与 selected 一样好。但是我无法在第三行获得其他值,而不是我只获得第一行值。任何人都可以提供正确的方法吗?
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>
控制器
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);
}
}
随着你的更新循环改变后,这里的贷款 Id 只打印与 selected 值相同,但对于其他内容,从第一行获取...
现在 loanId 打印在控制台中...
我尝试了下面的方法来获取复选框的位置,但这里的 loanId 也只是一样,其他都是从第一行开始的。
我正在寻找,如果第一个复选框被 selected 那么 loanId 将是 1,如果第三个复选框和第四个复选框被 selected 那么它将是 3,4..任何帮助都会不胜感激..
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] );
}
}
}
您的检索逻辑有问题。您正在从 int i = 0;
中检索所有其他值,这就是它显示第一个值的原因。尝试下面给定的循环。
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);
}