Spring mvc 如何在DataTables jqueryPlugin中实现排序? @RequestMapping(value=“/CustomerData”,method=RequestMethod.GET) public@ResponseBody字符串customerData( HttpServletRequest请求,HttpServletResponse响应,模型, @RequestParam(value=“order”,required=false,defaultValue=“ASC”)字符串顺序, @RequestParam(value=“orderBy”,required=false,defaultValue=“customerId”)字符串orderBy)引发IOException{ //从客户端获取页码 整数页码=0; if(null!=request.getParameter(“iDisplayStart”)) pageNumber=(Integer.valueOf(request.getParameter(“iDisplayStart”))/10)+1; //获取搜索参数 字符串searchParameter=request.getParameter(“sSearch”); 字符串orderByVal=request.getParameter(“iSortCol_0”); 字符串orderVal=request.getParameter(“sSortDir_0”); 如果(!Employee.isEmptyString(orderByVal)) { 如果(!Employee.isEmptyString(orderVal)) { order=orderVal; } 其他的 { order=“desc”; } if(orderByVal.equalsIgnoreCase(“0”)) orderBy=“customerId”; else if(orderByVal.equalsIgnoreCase(“1”)) orderBy=“customerName”; else if(orderByVal.equalsIgnoreCase(“2”)) orderBy=“电话号码”; else if(orderByVal.equalsIgnoreCase(“3”)) orderBy=“区域”; else if(orderByVal.equalsIgnoreCase(“4”)) orderBy=“城市”; 其他的 { orderBy=“customerId”; order=“desc”; } } //获取页面显示长度 Integer pageDisplayLength=Integer.valueOf(request.getParameter(“iDisplayLength”); 整数记录=(pageNumber-1)*pageDisplayLength; //创建页面列表数据 List customerList=customerService.getCustomerList(记录,页面显示长度); customerList=getCustomerListBasedOnSearchParameter(searchParameter,customerList); customerList=getSortedCustomer(customerList、order、orderBy); setContentType(“应用程序/Json”); 整数计数=空; if(customerList!=null&&customerList.size()>0){ count=customerList.size(); }否则{ count=customerService.count(); customerList=customerService.getCustomerList(记录,页面显示长度); } model.addAttribute(“订单”,订单); model.addAttribute(“orderBy”,orderBy); CustomerJsonObject customerJson=新CustomerJsonObject(); //设置总显示记录 customerJson.setiTotalDisplayRecords(计数); //创总记录 customerJson.setiTotalRecords(计数); customerJson.setAaData(customerList); Gson Gson=new GsonBuilder().setPrettyPrinting().create(); 字符串json=gson.toJson(customerJson); 返回json; }
//上面的代码是我的控制器代码。 但它并没有在我的项目中发挥作用。 如果我的控制器有什么错误,请告诉我。我已经试过了,但是我的控制器不可能。我能知道解决方法吗Spring mvc 如何在DataTables jqueryPlugin中实现排序? @RequestMapping(value=“/CustomerData”,method=RequestMethod.GET) public@ResponseBody字符串customerData( HttpServletRequest请求,HttpServletResponse响应,模型, @RequestParam(value=“order”,required=false,defaultValue=“ASC”)字符串顺序, @RequestParam(value=“orderBy”,required=false,defaultValue=“customerId”)字符串orderBy)引发IOException{ //从客户端获取页码 整数页码=0; if(null!=request.getParameter(“iDisplayStart”)) pageNumber=(Integer.valueOf(request.getParameter(“iDisplayStart”))/10)+1; //获取搜索参数 字符串searchParameter=request.getParameter(“sSearch”); 字符串orderByVal=request.getParameter(“iSortCol_0”); 字符串orderVal=request.getParameter(“sSortDir_0”); 如果(!Employee.isEmptyString(orderByVal)) { 如果(!Employee.isEmptyString(orderVal)) { order=orderVal; } 其他的 { order=“desc”; } if(orderByVal.equalsIgnoreCase(“0”)) orderBy=“customerId”; else if(orderByVal.equalsIgnoreCase(“1”)) orderBy=“customerName”; else if(orderByVal.equalsIgnoreCase(“2”)) orderBy=“电话号码”; else if(orderByVal.equalsIgnoreCase(“3”)) orderBy=“区域”; else if(orderByVal.equalsIgnoreCase(“4”)) orderBy=“城市”; 其他的 { orderBy=“customerId”; order=“desc”; } } //获取页面显示长度 Integer pageDisplayLength=Integer.valueOf(request.getParameter(“iDisplayLength”); 整数记录=(pageNumber-1)*pageDisplayLength; //创建页面列表数据 List customerList=customerService.getCustomerList(记录,页面显示长度); customerList=getCustomerListBasedOnSearchParameter(searchParameter,customerList); customerList=getSortedCustomer(customerList、order、orderBy); setContentType(“应用程序/Json”); 整数计数=空; if(customerList!=null&&customerList.size()>0){ count=customerList.size(); }否则{ count=customerService.count(); customerList=customerService.getCustomerList(记录,页面显示长度); } model.addAttribute(“订单”,订单); model.addAttribute(“orderBy”,orderBy); CustomerJsonObject customerJson=新CustomerJsonObject(); //设置总显示记录 customerJson.setiTotalDisplayRecords(计数); //创总记录 customerJson.setiTotalRecords(计数); customerJson.setAaData(customerList); Gson Gson=new GsonBuilder().setPrettyPrinting().create(); 字符串json=gson.toJson(customerJson); 返回json; },spring-mvc,datatables,Spring Mvc,Datatables,//上面的代码是我的控制器代码。 但它并没有在我的项目中发挥作用。 如果我的控制器有什么错误,请告诉我。我已经试过了,但是我的控制器不可能。我能知道解决方法吗 @RequestMapping(value = "/CustomerData", method = RequestMethod.GET) public @ResponseBody String customerData( HttpServletRequest reque
@RequestMapping(value = "/CustomerData", method = RequestMethod.GET)
public @ResponseBody String customerData(
HttpServletRequest request,HttpServletResponse response,Model model,
@RequestParam(value = "order",required = false, defaultValue = "ASC") String order,
@RequestParam(value = "orderBy",required = false, defaultValue = "customerId") String orderBy) throws IOException {
//Fetch the page number from client
Integer pageNumber = 0;
if (null != request.getParameter("iDisplayStart"))
pageNumber = (Integer.valueOf(request.getParameter("iDisplayStart"))/10)+1;
//Fetch search parameter
String searchParameter = request.getParameter("sSearch");
String orderByVal = request.getParameter("iSortCol_0");
String orderVal = request.getParameter("sSortDir_0");
if(!Employee.isEmptyString(orderByVal))
{
if(!Employee.isEmptyString(orderVal))
{
order = orderVal;
}
else
{
order = "desc";
}
if(orderByVal.equalsIgnoreCase("0"))
orderBy = "customerId";
else if(orderByVal.equalsIgnoreCase("1"))
orderBy = "customerName";
else if(orderByVal.equalsIgnoreCase("2"))
orderBy = "phoneNo";
else if(orderByVal.equalsIgnoreCase("3"))
orderBy = "area";
else if(orderByVal.equalsIgnoreCase("4"))
orderBy = "city";
else
{
orderBy = "customerId";
order = "desc";
}
}
//Fetch Page display length
Integer pageDisplayLength = Integer.valueOf(request.getParameter("iDisplayLength"));
Integer record = (pageNumber-1)*pageDisplayLength;
//Create page list data
List<Customer> customerList = customerService.getCustomerList(record,pageDisplayLength);
customerList = getCustomerListBasedOnSearchParameter(searchParameter, customerList);
customerList = getSortedCustomer(customerList, order,orderBy);
response.setContentType("application/Json");
Integer count= null;
if(customerList != null && customerList.size() > 0){
count=customerList.size();
}else{
count=customerService.count();
customerList = customerService.getCustomerList(record,pageDisplayLength);
}
model.addAttribute("order", order);
model.addAttribute("orderBy", orderBy);
CustomerJsonObject customerJson=new CustomerJsonObject();
//Set Total display record
customerJson.setiTotalDisplayRecords(count);
//Set Total record
customerJson.setiTotalRecords(count);
customerJson.setAaData(customerList);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(customerJson);
return json;
}
//我已经用下面的方法实现了,但是我不知道如何通过json对象将这些值发送到jsp。
公共列表getSortedCustomer(列表客户列表,
最终字符串顺序,最终字符串顺序){
最终布尔描述=顺序等于(“描述”);
最终整数排序方向=描述?-1:1;
Collections.sort(customerList,newcomparator(){
@凌驾
公共整数比较(客户c1、客户c2){
if(orderBy.equals(“customerId”)){
返回c1.getCustomerId().compareTo(c2.getCustomerId())*sortDirection;
}else if(orderBy.equals(“customerName”)){
返回c1.getCustomerName().compareTo(c2.getCustomerName())*sortDirection;
}else if(orderBy.equals(“phoneNo”)){
返回c1.getPhoneNo().compareTo(c2.getPhoneNo())*sortDirection;
}else if(orderBy.equals(“email_Id”)){
返回c1.getEmail_Id().compareTo(c2.getEmail_Id())*sortDirection;
}else if(orderBy.equals(“面积”)){
返回c1.getArea().compareTo(c2.getArea())*sortDirection;
}else if(orderBy.equals(“城市”)){
返回c1.getCity().compareTo(c2.getCity())*sortDirection;
}
返回0;
}
});
返回客户列表;
}
到底是什么问题?只是排序不起作用还是表格根本没有填充?我已经编辑了代码。再次检查后,请回答我的问题。排序不起作用。。
//I have implemented in the below way but i dont know how to send these values to jsp through json object.
public List<Customer> getSortedCustomer(List<Customer> customerList,
final String order, final String orderBy) {
final boolean desc = order.equals("DESC");
final int sortDirection=desc ? -1:1;
Collections.sort(customerList, new Comparator<Customer>() {
@Override
public int compare(Customer c1, Customer c2) {
if (orderBy.equals("customerId")) {
return c1.getCustomerId().compareTo(c2.getCustomerId()) * sortDirection;
} else if (orderBy.equals("customerName")) {
return c1.getCustomerName().compareTo(c2.getCustomerName()) * sortDirection;
} else if (orderBy.equals("phoneNo")) {
return c1.getPhoneNo().compareTo(c2.getPhoneNo()) * sortDirection;
} else if (orderBy.equals("email_Id")) {
return c1.getEmail_Id().compareTo(c2.getEmail_Id()) * sortDirection;
} else if (orderBy.equals("area")) {
return c1.getArea().compareTo(c2.getArea()) * sortDirection;
}else if (orderBy.equals("city")) {
return c1.getCity().compareTo(c2.getCity()) * sortDirection;
}
return 0;
}
});
return customerList;
}