Java Spring MVC+;jQuery数据表+;JSON响应
我有一个SpringMVC应用程序,它使用jQuery数据表来呈现数据。 下面是我的类和javascript代码 Employee.javaJava Spring MVC+;jQuery数据表+;JSON响应,java,jquery,spring-mvc,datatables,Java,Jquery,Spring Mvc,Datatables,我有一个SpringMVC应用程序,它使用jQuery数据表来呈现数据。 下面是我的类和javascript代码 Employee.java public class Employee { private int empId ; private String firstName; private String lastName ; // getters and setters } public class EmployeeResponse { pri
public class Employee {
private int empId ;
private String firstName;
private String lastName ;
// getters and setters
}
public class EmployeeResponse {
private List<Employee> empList ;
// getters and setters
}
@Controller
@RequestMapping("/admin")
public class EmployeeController {
@RequestMapping(value = "/get-all-employee", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public EmployeeResponse getAllEmployee(HttpServletRequest request) {
EmployeeResponse response = new EmployeeResponse();
try {
response = getAllEmployeeList(); // returns response object
} catch (Exception e) {
logger.error("DCConsoleController::createNewStream exception: " + com.priceline.utils.StringUtils.getStackTrace(e));
return null;
}
return response ;
}
}
EmployeeResponse.java
public class Employee {
private int empId ;
private String firstName;
private String lastName ;
// getters and setters
}
public class EmployeeResponse {
private List<Employee> empList ;
// getters and setters
}
@Controller
@RequestMapping("/admin")
public class EmployeeController {
@RequestMapping(value = "/get-all-employee", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public EmployeeResponse getAllEmployee(HttpServletRequest request) {
EmployeeResponse response = new EmployeeResponse();
try {
response = getAllEmployeeList(); // returns response object
} catch (Exception e) {
logger.error("DCConsoleController::createNewStream exception: " + com.priceline.utils.StringUtils.getStackTrace(e));
return null;
}
return response ;
}
}
emp.jsp
<div class="row">
<table id="ldapStreamTable" class="table">
<thead>
<tr>
<th>Emp Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
</table>
</div>
我将您的示例
ajax
数据保存在中,并将其作为ajax
源代码提供。您将该部分保留在方法中,该方法返回json
响应,但不是将empList
作为json
响应中的基本根,而是将其替换为data
,就像我在上面的github
json
文件中所做的那样。现在,除了ajaxSource
之外,您还需要将列
映射到数据表
,如下所示:
"columns": [
{ "data": "empId"},//should match column value in json file
{ "data": "firstName"},//should match column value in json file
{ "data": "lastName"}//should match column value in json file
]
您是否考虑过使用Gson或类似的库?我从controller得到的响应已经是使用Jackson API的json格式。我无法弄清楚的是如何将json响应(一个列表对象)绑定到UIN上的列中。你能发布你的json
响应吗?@GuruprasadRao-用示例json响应更新了这个问题。我只是想出了另一种方法,通过设置“sAjaxDataProp”:“streamList”和“aoColumns”:[{mDataProp:“empId”},{mDataProp:“lastName”},{mDataProp:“firstName”}]。但是,我使用mRender选项生成一个按钮,在呈现表之后,我如何将事件绑定到它。fRowCallback?给您的按钮一个类
,并将事件添加到该类
,这正是我所做的,但不知怎的,单击事件没有得到注册。我认为您没有在此处执行事件委派
。。由于您要将稍后部分的按钮控件呈现给DOM
,因此必须执行事件委派
操作,例如附加一些元素
控件,该元素包含按钮
元素,并且已在DOM
加载中注册。例如:文档
,它是根目录。这样做$(document).on('click','.yourButtonClass',function(){//click event methods});
当然,这可能也可以。但我最终使用了fnDrawCallback,并像前面提到的那样将click事件绑定到类。使用fnDrawCallback选项似乎是一种干净的方法。感谢您的帮助。