场景:页面需要选择多个职称,然后表格需要根据选择的职称查询数据库.
主要问题有:怎样传递多个类型相同的值到后台
方法:页面需要定义数组,将值存入数组进行传递,传递后台需要注意,这里是数组所以@RequestParam则必须使用,并且value的值是数组形式,另外需要的注意的是,为了没有选择职称的时候能够默认的查询数据库的所有人员,则需要使用@RequestParam的default属性,并且设的值也很关键,这里因为是字符串数组接受参数,所以我们将默认值设置为空串.
页面代码:
script type="text/javascript">
$('#btn').bind('click', function () {
//定义一个数组
var checkValues = new Array();
//获取页面所有被选中的复选框,遍历复选框,然后获取其中的值并存入定义的数组中
$('input[name="certificates"]:checked').each(function () {
checkValues.push($(this).val());
});
//让数据表格带着参数去查询
$('#dg').datagrid('load',{
certificates: checkValues
});
});
</script>
后台代码:
//获取员工信息,用于页面展示
@RequestMapping("showEmployees")
@ResponseBody
public Map<String, Object> showEmployees(Integer page, Integer rows,
@RequestParam(value = "certificates[]",defaultValue = "") String[] certificates) {
//调用service查询数据
PageInfo<Employee> pageInfo = null;
if (certificates != null && certificates.length > 0) {
pageInfo = employeeService.showEmployees(page, rows, certificates);
}else {
pageInfo = employeeService.showEmployees(page,rows);
}
Map<String, Object> map = new HashMap<>();
map.put("total", pageInfo.getTotal());
map.put("rows", pageInfo.getList());
return map;
}