javascript - how to pass the multi-select values from JSP to controller without making use of any Model
问题描述
Could anyone please refer me any article which clearly explains how to pass the multi-select values from JSP to controller without making use of any Model.
My requirement : on a webpage there is just a multi-select List box and submit button. The multiselect list box contains the OrderId and CustomerCode separated by "|"
Example:
- Order1|Customer1
- Order2|Customer2
- Order3|Customer3
- Order4|Customer4
- Order5|Customer5
The user can select 1 or more and click on submit button. I need to query my order table and then generate a jasper report. So, my plan is to take the values from JSP into Controller (in form of List or OrderIds) Pass that to the Service and then create the JASPER using the corresponding jrxml.
It would be great to have some suggestion/advice from you on this.
解决方案
use the following technologies,
- java script - get the selected values from multi-select list box in the jsp
- ajax - pass the selected values to the controller from jsp
additionally use the spring framework for creating a java web application
multi-select list in the form.jsp
<select id='category' multiple='multiple'>
<option value='Order1|Customer1'>Order1|Customer1</option>
<option value='Order2|Customer2'>Order2|Customer2</option>
<option value='Order3|Customer3'>Order3|Customer3</option>
<option value='Order4|Customer4'>Order4|Customer4</option>
<option value="Order5|Customer5">Order5|Customer5</option>
</select>
java script function for collect the selected multi values from jsp and pass it to the controller
function setData(){
var selectedValues= [];
$('#category:selected').each(function(i, selected) {
selectedValues[i] = $(category).val();
});
$.ajax({
type: 'POST',
dataType: 'json',
data {'selectedValues':selectedValues.toString()},
url: '/Project_name/add'
});
}
call the above js function from click event of the submit button
<button onclick="setData()">Submit</button>
above js function call the insertData()
method in the controller (MyController) and pass the selected values to it
@Controller
public class MyController {
@RequestMapping(value="/add", method=RequestMethod.POST)
public void insertData(@RequestParam(value="selectedValues") ArrayList<String> selectedValues){
//query order table and then generate a jasper report so on
}
}
推荐阅读
- python - ValueError:调用 cross_val_score 时发现未知类别
- python - 重复运行该程序的不同 k-means 结果
- wordpress - 如何在 Woocommerce 中获得可变产品的追加销售?
- javascript - 窗口大小较小时容器重叠
- javascript - Node.js Sequelize连接两个表并使用创建的数据创建第三个表并计算第一列相同数量的总和
- python - 2d numpy 数组中的平方和(形成一个正方形的 4 个值)。Python
- javascript - 根据数据库中的内容重定向http请求的正确方法是什么?
- c# - 如何在不下载的情况下读取位于 Azure 容器中的压缩 txt 文件(blob)?
- javascript - 如何在 HTML 元素(基于 JSON)中找到负责更改数据的 javascript 代码?
- flutter - Dart,后面带感叹号的标识符