简体   繁体   中英

How to pass variable number of parameters from jsp page to the java controller

I have a jsp page and a controller with fillowing functionality:

  java controller code: @Controller public class AddNewItemController { @RequestMapping(value = "/newItem/submit", method = RequestMethod.POST) public String getDataForInterval4(@RequestParam("itemId") String itemId, @RequestParam("product1SkusCnt") String product1SkusCnt, @RequestParam("itemName") String itemName, HttpServletRequest request) { return "ItemSubmitted"; } } 
 my main jsp file that submits to controller: <center> <table> <tr> <td>How many items do you have?</td> <td> <input type="number" name="productsCnt" id="productsCnt" size="2" min="1" value="1" onchange="productCount()"/> </td> </tr> </table> </center> <form action="/newItem/submit" method="post"> <br /><br /> <div id="outerDivContainer"> <div id="product1Div" name="product1Div" > <hr /> Product 1 Name: <input id="product1Name" /> Product 1 ID: <input id="product1ID" /> How many SKUs of Product 1? <input id="product1SkusCnt" type="number" size="2" min="1" value="1" onchange="skusCount(1)"/> <br /><br /> <div id="skusContainer1"> <div id="sku1Div"> SKU 1 Name: <input id="sku1"/> </div> <br /> </div> </div> </div> <hr /> <input type="submit" value="Submit" /> </form> <script> function productCount() { console.log("onchange product"); document.getElementById('outerDivContainer').innerHTML = ""; var cnt = document.getElementById('productsCnt').value; console.log("cnt="+cnt); for (i=0;i<cnt;i++){ var newEl = document.createElement('div'); newEl.class='prodRow'; j=i+1; newEl.innerHTML = '<div id="product'+j+'Div"><hr /> Product '+j+' Name: <input id="product'+j+'Name" /> Product '+j+' ID: <input id="product'+j+'ID" /> How many SKUs of Product '+j+'? <input id="product' +j+'SkusCnt" type="number" size="2" min="1" value="1" onchange="skusCount('+ j+')" /> <br /><br /> <div id="skusContainer'+j+'"><div id="sku1Div"> SKU 1 Name: <input id="sku1"/></div> <br /> </div></div>'; document.getElementById('outerDivContainer').appendChild(newEl); } } function skusCount(productId){ console.log("onchange skus, product id= "+productId+";"); var skusCnt = document.getElementById('product'+productId+'SkusCnt').value; console.log("skusCnt="+skusCnt); document.getElementById('skusContainer'+productId).innerHTML = ""; for (i=0;i<skusCnt;i++){ var newEl = document.createElement('div'); newEl.class='skuRow'; j=i+1; newEl.innerHTML = '<div id="sku'+j+'Div">SKU '+j+' Name: <input id="sku'+j+'" /> </div> <br />'; document.getElementById('skusContainer'+productId).appendChild(newEl); } } </script> 

ItemSubmitted.jsp is just a jsp file that confirms successful item submission. The problem is I don't know how many items will be passed to the controller, or how many skus each item might have. What would be a suggested approach to this problem? Thanks in advance!

The answer to my question is much simpler than I expected. All I have to do is just know how many parameters I will pass and their names. I don't have to specify it in the controller as @RequestParam("itemId") String itemId, @RequestParam("product1SkusCnt") etc.

Instead I should only pass a request as an argument:

@RequestMapping(value = "/newItem/submit", method = RequestMethod.POST) public String getDataForInterval4(HttpServletRequest request) { return "ItemSubmitted"}

I can call each of those passed parameters in the controller by doing the following:

request.getParameter("product"+i+"SkusCnt"))

Just need to make sure that I pass the total count of products also.

Hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM