复选框和从jsp到java控制器文件的相应行的值 [英] checkbox and respective row's values from jsp to java controller file

查看:183
本文介绍了复选框和从jsp到java控制器文件的相应行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在spring mvc工作,现在专注于从jsp插入操作到执行控制器。



我有一个来自搜索查询的值列表在数据库中。我只是想保存少数行,而不是所有的。为此,我放一个复选框,以选择哪些行需要删除,以便它只能插入表中。



如果我在该表中选择第三行,我得到的chackbox值只有选择好。但我不能得到其他值在第三行,而不是我只得到第一行的值。



JSP:

 < div id =login_block> 
< center>
< span style =font-size:2em;> Funder分配< / span>< / center>
< div style =margin-top:30px;>
< form id =saveAssignmethod =POSTname =saveAssign
action =saveAssign.htmautocomplete =on>

< label class =bold_textstyle =margin-right:5%; margin-left:10%;>贷款类型< / label&
< select name =producttypeid =producttypestyle =width:29%;>
< option value =0> - 选择 - < / option>
< c:forEach var =entryitems =$ {productList}>
< option value =$ {entry.key}> $ {entry.value}< / option>
< / c:forEach>
< / select>< br />< br />
< table border = 1>
< thead>
< tr>
< th>选择< / th>
< th>贷款A / c否< / th>
< th style =width; 1%;>类型< / th>
< th> Funder< / th>
< th>目的< / th>
< / tr>
< / thead>
< tbody>
< c:forEach var =loanitems =$ {loans}>
< tr>
< td>< input type =checkboxname =loanIdvalue =$ {loan.id}>< / td>
< td>< input type =textname =loanAccNovalue =$ {loan.loanAccNo}readonly =readonly>< / td&
< td>< input type =textname =productvalue =$ {loan.product.name}readonly =readonly>< / td&
< td>< select id =funderIdname =funderId>
< option value =0> - 选择 - < / option>
< c:forEach var =funditems =$ {fundMap}>
< option value =$ {fund.key}> $ {fund.value}< / option>
< / c:forEach>
< / select>< / td>
< td>< select id =purposename =purposestyle =width:75%;>
< option value =0> - 选择 - < / option>
< c:forEach var =purposeitems =$ {fundProdPurpose}>
< option value =$ {purpose.id}> $ {purpose.id}< / option>
< / c:forEach>
< / select>< / td>
< / tr>
< / c:forEach>
< / tbody>
< / table>
< div>
< input class =login_block_login_btntype =submitname =submitstyle =margin-left:0%;
value =Saveid =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保存(HttpServletRequest请求,
HttpServletResponse响应)throws异常{
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 = 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,无法添加资助者详细信息,请求);
}
}

更改为更新循环后,



现在loanId是打印在控制台...



我试着用下面的方法获取复选框的位置,但这里的loanId只是一样,其他都是从第一行。



我正在寻找,如果第一个复选框被选中,那么loanId将是1,如果第三个复选框和第四个复选框,那么它将是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)
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);
}


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] ); 
                    } 
            }
        }

解决方案

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);    
}

这篇关于复选框和从jsp到java控制器文件的相应行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆