一 DeptController
package org.fkit.oa.identity.controller;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.fkit.oa.identity.domain.Dept;
import org.fkit.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/identity/dept")
public class DeptController {
/** 定义业务层对象 */
@Resource // by type
private IdentityService identityService;
@RequestMapping(value="/selectAll")
public String selectAll(Model model){
try {
List<Dept> depts = identityService.getAllDepts();
System.out.println("depts:"+depts);
model.addAttribute("depts", depts);
} catch (Exception e) {
e.printStackTrace();
}
return "aaa";
}
// 异步方法 写数据回去
@ResponseBody
@RequestMapping(value="/getAllDeptsAndJobsAjax",produces="application/json; charset=UTF-8")
public Map<String, Object> getAllDeptsAndJobsAjax(){
try {
Thread.sleep(2000);
Map<String, Object> rs = identityService.getAllDeptsAndJobsAjax();
System.out.println("rs:"+rs);
return rs ;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
二 LoginController
package org.fkit.oa.identity.controller;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.fkit.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class LoginController {
/** 1.定义业务层对象 */
@Resource // by type
private IdentityService identityService;
@ResponseBody // 异步请求的响应结果
@RequestMapping(value="/loginAjax",produces="application/json; charset=UTF-8")
public Map<String, Object> login(@RequestParam("userId")String userId,
@RequestParam("passWord")String passWord
,@RequestParam("vcode")String vcode
,HttpSession session){
try {
Map<String, Object> params = new HashMap<>();
params.put("userId", userId);
params.put("passWord", passWord);
params.put("vcode", vcode);
params.put("session", session);
// 响应数据啊,写回去数据
Map<String, Object> result = identityService.login(params);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
三 LogoutController
package org.fkit.oa.identity.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LogoutController {
@RequestMapping(value="/logout")
public String logout(HttpSession session){
try {
/** 注销当前会话 */
session.invalidate();
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/oa/login";
}
}
四 ModuleController
package org.fkit.oa.identity.controller;
import java.util.List;
import javax.annotation.Resource;
import org.fkit.common.util.pager.PageModel;
import org.fkit.oa.identity.domain.Module;
import org.fkit.oa.identity.service.IdentityService;
import org.fkit.oa.identity.vo.TreeData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 模块菜单
*/
@Controller
@RequestMapping("/identity/module")
public class ModuleController {
@Resource // by type
private IdentityService identityService;
@RequestMapping(value="/mgrModule")
public String mgrModule(){
return "identity/module/mgrModule";
}
@ResponseBody
@RequestMapping(value="/loadAllModuleTrees",produces="application/json; charset=UTF-8")
public List<TreeData> loadAllModuleTrees(){
try {
return identityService.loadAllModuleTrees();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RequestMapping(value="/getModulesByParent")
public String getModulesByParent(String parentCode,PageModel pageModel,Model model){
try {
List<Module> sonModules = identityService.getModulesByParent(parentCode,pageModel);
model.addAttribute("modules", sonModules);
model.addAttribute("parentCode", parentCode);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/module/module";
}
@RequestMapping(value="/deleteModules")
public String deleteModules(String ids,Model model){
try {
identityService.deleteModules(ids);
model.addAttribute("tip", "删除成功");
} catch (Exception e) {
model.addAttribute("tip", "删除失败");
e.printStackTrace();
}
return "forward:/identity/module/getModulesByParent";
}
@RequestMapping(value="/addModule")
public String addModule(String parentCode,Module module ,Model model){
try {
identityService.addModule(parentCode,module);
model.addAttribute("tip", "添加成功");
model.addAttribute("parentCode", parentCode);
} catch (Exception e) {
model.addAttribute("tip", "添加失败");
e.printStackTrace();
}
return "identity/module/addModule";
}
@RequestMapping(value="/updateModule")
public String updateModule(Module module ,Model model){
try {
identityService.updateModule(module);
model.addAttribute("tip", "修改成功");
} catch (Exception e) {
model.addAttribute("tip", "修改失败");
e.printStackTrace();
}
return "identity/module/updateModule";
}
@RequestMapping(value="/showAddModule")
public String showAddModule(String parentCode,Model model){
model.addAttribute("parentCode", parentCode);
return "identity/module/addModule";
}
@RequestMapping(value="/showUpdateModule")
public String showUpdateModule(Module module ,Model model){
try {
module = identityService.getModuleByCode(module.getCode());
model.addAttribute("module", module);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/module/updateModule";
}
}
五 PopedomController
package org.fkit.oa.identity.controller;
import java.util.List;
import javax.annotation.Resource;
import org.fkit.oa.identity.domain.Module;
import org.fkit.oa.identity.domain.Role;
import org.fkit.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/identity/popedom")
public class PopedomController {
/** 1.定义业务层对象 */
@Resource // by type
private IdentityService identityService;
@RequestMapping(value="/mgrPopedom")
public String mgrPopedom(Role role,Model model){
try {
role = identityService.getRoleById(role.getId());
model.addAttribute("role", role);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/role/bindPopedom/mgrPopedom";
}
@RequestMapping(value="/getOperasByParent")
public String getOperasByParent(
@RequestParam("parentCode")String parentCode,
@RequestParam("moduleName")String moduleName,
Role role,Model model){
try {
List<Module> sonModules = identityService.getModulesByParent(parentCode);
/** 还应该查询出当前role在当前模块下parentCode拥有哪些操作权限
* 去控制界面的权限显示
* 查询出当前角色role在parentCode模块下拥有的操作编号即可。
* */
List<String> roleModuleOperasCodes = identityService.getRoleModuleOperasCodes(role,parentCode);
model.addAttribute("modules", sonModules);
model.addAttribute("moduleName", moduleName);
model.addAttribute("parentCode", parentCode);
model.addAttribute("roleModuleOperasCodes", roleModuleOperasCodes+"");
} catch (Exception e) {
e.printStackTrace();
}
return "identity/role/bindPopedom/operas";
}
@RequestMapping(value="/bindPopedom")
public String bindPopedom(@RequestParam("codes")String codes,
@RequestParam("parentCode")String parentCode,Role role,Model model){
try {
identityService.bindPopedom(codes,role,parentCode);
model.addAttribute("tip", "绑定成功");
} catch (Exception e) {
model.addAttribute("tip", "绑定失败");
e.printStackTrace();
}
return "forward:/identity/popedom/getOperasByParent";
}
}
六 RequestController
package org.fkit.oa.identity.controller;
import java.util.List;
import javax.annotation.Resource;
import org.fkit.oa.identity.dto.UserModule;
import org.fkit.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/oa")
public class RequestController {
/** 1.定义业务层对象 */
@Resource // by type
private IdentityService identityService;
@RequestMapping(value="/login")
public String requestLogin(){
System.out.println("登录成功了!");
return "login";
}
@RequestMapping(value="/main")
public String requestMain(Model model){
try {
//查询出当前用户拥有的所有模块权限
List<UserModule> userModules = identityService.getUserPopedomModules();
model.addAttribute("userPopedomModules", userModules);
} catch (Exception e) {
e.printStackTrace();
}
return "main";
}
@RequestMapping(value="/home")
public String requestHome(){
return "home";
}
}
七 RoleController
package org.fkit.oa.identity.controller;
import java.util.List;
import javax.annotation.Resource;
import org.fkit.common.util.pager.PageModel;
import org.fkit.oa.identity.domain.Role;
import org.fkit.oa.identity.domain.User;
import org.fkit.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/identity/role")
public class RoleController {
/** 1.定义业务层对象 */
@Resource // by type
private IdentityService identityService;
@RequestMapping(value="/selectRole")
public String selectRole(PageModel pageModel,Model model){
try {
List<Role> roles = identityService.getRoleByPager(pageModel);
model.addAttribute("roles", roles);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/role/role";
}
@RequestMapping(value="/addRole")
public String addRole(Role role,Model model){
try {
identityService.addRole(role);
model.addAttribute("tip","添加成功");
} catch (Exception e) {
model.addAttribute("tip","添加失败");
e.printStackTrace();
}
return "identity/role/addRole";
}
@RequestMapping(value="/deleteRole")
public String deleteRole(String ids,Model model){
try {
identityService.deleteRole(ids);
model.addAttribute("tip","删除成功");
} catch (Exception e) {
model.addAttribute("tip","删除失败");
e.printStackTrace();
}
return "forward:/identity/role/selectRole";
}
@RequestMapping(value="/showAddRole")
public String showAddRole(){
return "identity/role/addRole";
}
@RequestMapping(value="/showUpdateRole")
public String showUpdateRole(Role role , Model model){
try {
role = identityService.getRoleById(role.getId());
model.addAttribute("role", role);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/role/updateRole";
}
@RequestMapping(value="/updateRole")
public String updateRole(Role role , Model model){
try {
identityService.updateRole(role);
model.addAttribute("tip", "修改成功");
} catch (Exception e) {
model.addAttribute("tip", "修改失败");
e.printStackTrace();
}
return "identity/role/updateRole";
}
@RequestMapping(value="/selectRoleUser")
public String selectRoleUser(Role role ,PageModel pageModel, Model model){
try {
/** 查询属于这个角色下的所有用户 */
List<User> users = identityService.selectRoleUser(role,pageModel);
role = identityService.getRoleById(role.getId());
model.addAttribute("users", users);
model.addAttribute("role", role);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/role/bindUser/roleUser";
}
@RequestMapping(value="/showBindUser")
public String selectNotRoleUser(Role role ,PageModel pageModel, Model model){
try {
/** 查询不属于这个角色下的所有用户 */
List<User> users = identityService.selectNotRoleUser(role,pageModel);
role = identityService.getRoleById(role.getId());
model.addAttribute("users", users);
model.addAttribute("role", role);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/role/bindUser/bindUser";
}
@RequestMapping(value="/bindUser")
public String bindUser(Role role,String ids, Model model){
try {
identityService.bindUser(role,ids);
model.addAttribute("tip","绑定成功");
} catch (Exception e) {
model.addAttribute("tip","绑定失败");
e.printStackTrace();
}
return "forward:/identity/role/showBindUser";
}
@RequestMapping(value="/unBindUser")
public String unBindUser(Role role,String ids, Model model){
try {
identityService.unBindUser(role,ids);
model.addAttribute("tip","解绑成功");
} catch (Exception e) {
model.addAttribute("tip","解绑失败");
e.printStackTrace();
}
return "forward:/identity/role/selectRoleUser";
}
}
八 UserController
package org.fkit.oa.identity.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.fkit.common.util.pager.PageModel;
import org.fkit.oa.identity.domain.User;
import org.fkit.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/identity/user")
public class UserController {
/** 1.定义业务层对象 */
@Resource // by type
private IdentityService identityService;
@RequestMapping(value="/updateSelf")
public String updateSelf(User user,Model model,HttpSession session){
try {
identityService.updateSelf(user,session);
model.addAttribute("tip", "修改成功!");
} catch (Exception e) {
model.addAttribute("tip", "修改失败!");
e.printStackTrace();
}
return "home";
}
@RequestMapping(value="/selectUser")
public String selectUser(User user ,HttpServletRequest request, PageModel pageModel,Model model){
try {
/** 1.自己处理 :只需要处理get请求的参数
* post请求的参数不会乱码
* */
if(request.getMethod().toLowerCase().indexOf("get")!=-1){
if(user!=null && !StringUtils.isEmpty(user.getName())){
String name = user.getName();
/**
* 浏览器到后台乱码
* */
name = new String(name.getBytes("ISO-8859-1") , "UTF-8");
user.setName(name);
}
}
List<User> users = identityService.getUsersByPager(user,pageModel);
model.addAttribute("users", users);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/user/user";
}
@RequestMapping(value="/deleteUser")
public String deleteUser(String ids ,Model model){
try {
/** 批量删除 */
identityService.deleteUserByUserIds(ids);
model.addAttribute("tip", "删除成功");
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("tip", "删除失败");
}
return "forward:/identity/user/selectUser";
}
@RequestMapping(value="/showAddUser")
public String showAddUser(){
return "identity/user/addUser";
}
@ResponseBody
@RequestMapping(value="/isUserValidAjax")
public String isUserValid(String userId){
try {
return identityService.isUserValidAjax(userId);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RequestMapping(value="/addUser")
public String addUser(User user,Model model){
try {
/** 批量删除 */
identityService.addUser(user);
model.addAttribute("tip", "添加成功");
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("tip", "添加失败");
}
return "identity/user/addUser";
}
@RequestMapping(value="/updateUser")
public String updateUser(User user,Model model){
try {
/** 批量删除 */
identityService.updateUser(user);
model.addAttribute("tip", "修改成功");
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("tip", "修改失败");
}
return "identity/user/updateUser";
}
@RequestMapping(value="/activeUser")
public String activeUser(User user,Model model){
try {
/** 激活用户 */
identityService.activeUser(user);
model.addAttribute("tip", user.getStatus()==1?"激活成功":"冻结成功");
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("tip", user.getStatus()==1?"激活失败":"冻结失败");
}
return "forward:/identity/user/selectUser";
}
@RequestMapping(value="/showPreUser")
public String showPreUser(String userId,Model model){
try {
/** 批量删除 */
User user = identityService.getUserById(userId);
model.addAttribute("user", user);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/user/preUser";
}
@RequestMapping(value="/showUpdateUser")
public String showUpdateUser(String userId,Model model){
try {
/** 批量删除 */
User user = identityService.getUserById(userId);
model.addAttribute("user", user);
} catch (Exception e) {
e.printStackTrace();
}
return "identity/user/updateUser";
}
}
来源:https://blog.csdn.net/chengqiuming/article/details/102730178