UsersController.java
3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.project.demo.web;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.demo.domain.DataTablePageUtil;
import com.project.demo.domain.Users;
import com.project.demo.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping(value = "/users")
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping(method = RequestMethod.GET)
public String findAll(){
return "/users/index";
}
@RequestMapping(value = "/usersList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String usersList(final HttpSession session, final HttpServletRequest request){
final Map<String,Object> params = new HashMap<>();
DataTablePageUtil<Users> dataTable = new DataTablePageUtil<Users>(request);
System.out.print(request.toString());
System.out.println(dataTable.getPage_num()+"||"+dataTable.getPage_size());
PageHelper.startPage(dataTable.getPage_num(),dataTable.getPage_size());
// Integer limit = dataTable.getPage_size();
// Integer offset =( dataTable.getPage_num()-1)*limit;
// params.put("limit",limit);
// params.put("offset",offset);
// System.out.println(params.get("limit")+"|-------|"+params.get("offset"));
// PageHelper.startPage(dataTable.getPage_num(),dataTable.getPage_size());
final List<Users> usersList = usersService.search(params);
PageInfo<Users> pageInfo = new PageInfo<>(usersList,2);
System.out.println(pageInfo.toString());
//封装数据给DataTables
dataTable.setDraw(dataTable.getDraw());
dataTable.setData(pageInfo.getList());
dataTable.setRecordsTotal((int)pageInfo.getTotal());
dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
//返回数据到页面
String jsonObject = JSONObject.toJSONString(dataTable);
pageInfo.setStartRow(1);
pageInfo.setEndRow(10);
return jsonObject;
}
@RequestMapping(value = "/create")
public String create() {
return "/users/new";
}
@RequestMapping(value = "/new", method = RequestMethod.POST)
public String save(final HttpSession session, @RequestParam String name, @RequestParam String mobile,
@RequestParam String password){
Users currentUsers = (Users)session.getAttribute("users");
Users users = new Users();
users.setName(name);
users.setLevel(1);
users.setMobile(mobile);
users.setPassword(password);
usersService.save(users);
return "redirect:/users";
}
}