RoleJsonController.java
2.4 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.tianbo.controller.json;
import com.tianbo.common.ModelAndPage;
import com.tianbo.controller.base.BaseController;
import com.tianbo.model.Role;
import com.tianbo.model.RoleExample;
import com.tianbo.service.RoleService;
import com.tianbo.util.dao.Page;
import com.tianbo.util.json.ResponseModel;
import com.tianbo.util.tools.MD5Tools;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/role")
public class RoleJsonController extends BaseController{
//service类
@Autowired
private RoleService roleService;
@RequestMapping("list.json")
@RequiresRoles("admin")
@ResponseBody
public ModelAndPage list(@ModelAttribute Page frontPage, @ModelAttribute Role role) throws Exception{
RoleExample example = new RoleExample();
example.setOrderByClause("role_id");
//调用service方法得到用户列表
List<Role> roles = roleService.selectByExample(example);
ModelAndPage modelAndPage = new ModelAndPage(roles,new Page());
return modelAndPage;
}
@RequestMapping("roleExamlpe.json")
@ResponseBody
public Role example(){
return new Role();
}
@RequestMapping(value = "add.json",method = RequestMethod.POST)
@RequiresRoles("admin")
@ResponseBody
public ResponseModel add(@RequestBody Role role){
ResponseModel responseModel = new ResponseModel();
if (checkRole(role)){
int i = roleService.insert(role);
if(i>0){
responseModel.setStatus(200);
responseModel.setMsg("添加角色成功");
}else {
responseModel.setStatus(500);
responseModel.setMsg("添加角色失败");
}
}else {
responseModel.setStatus(500);
responseModel.setMsg("添加角色失败,已存在相同名称的角色");
}
return responseModel;
}
private Boolean checkRole( Role role){
if (roleService.checkRole(role)){
return Boolean.FALSE; //存在用户,返回false
}else {
return Boolean.TRUE; //不存在用户,返回true,可以添加用户
}
}
}