- //绑定用户列表
- function BindUser() {
- //从UserManagerServlet中的listUser()方法中拿到json数据。加上时间戳是为了防止缓存
- $.getJSON("servlet/UserManagerServlet?time=" + new Date().getTime(), { method: "ListUser"},
- function (data) {
- var s = "";//声明一个字符串来拼接table的HTML语句
- $("#tableEmployee tr:eq(0)").siblings().remove();//每次调用这个方法时会把除了标题列的所有数据删除,防止重复
- $.each(data, function (index, user) {
- if (data == "" || data == null) {
- s += "<tr><td colspan=9>没有任何记录!</td></tr>"
- } else {
- //拼接html
- s += "<tr><td>" + user.u_ID + "</td><td>" + user.u_LoginName + "</td><td>" + user.u_RealName + "</td>";
- s += "<td>" + user.u_Sex + "</td><td d_id='" + user.department.d_ID + "'>" + user.department.d_Name + "</td>"
- s += "<td>" + user.u_Email + "</td>";
- s += "<td>" + user.u_Tel + "</td>";
- s += "<td><input type='button' U_ID='" + user.u_ID + "' id='btnRole' class='ui-icon ui-icon-person' title='角色'></td>";
- s += "<td><input type='button' U_ID='" + user.u_ID + "' id='btnEdit' class='ui-icon ui-icon-wrench' title='编辑'></td>";
- s += "<td><input type='button' U_ID='" + user.u_ID + "' id='btnDelete' class='ui-icon ui-icon-trash ' title='删除'></td>";
- }
- });
- $("#tableEmployee").append(s);//将拼接好的html语句添加到table下面。则看到你要的界面了
- });
- }
复制代码 这个是后台代码- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String methodName=request.getParameter("method");
- if("ListUser".equals(methodName)){
- ListUser(request,response);
- }else if("DeleteUser".equals(methodName)){
- DeleteUser(request,response);
- }else if("UpdateUser".equals(methodName)){
- UpdateUser(request,response);
- }
-
-
-
- }
- //获取所有的用户列表
- private void ListUser(HttpServletRequest request,
- HttpServletResponse response)throws ServletException, IOException {
-
- response.setContentType("application/json;charset=UTF-8"); //getJSON方法使用get方法提交必须设置字符编码格式
-
- UserService service=new UserService();
-
- List<Users> listUser=service.getAllUsers();
-
- JSONArray json_user=JSONArray.fromObject(listUser); //将集合数据转换成json格式
-
- response.getWriter().print(json_user); //返回给客户端
-
- }
复制代码 这里面有不明白的可以问我。 |