项目简述:本项目是基于SSM的后台管理系统脚手架,面向接口编程。实现了菜单管理、角色管理、用户管理、日志管理,分页查看,不同的角色分配相应的权限,用户分配不同的角色登录后显示相应被分配的功能,使用了拦截器编程。
技术框架:SSM+easyUI+ajax+jsp
1.用户登录及日志管理
@RequestMapping("/admin/user")
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
/**
* 用户列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
Map queryMap = new HashMap();
model.addObject("roleList", roleService.findList(queryMap));
model.setViewName("user/list");
return model;
}
/**
* 获取用户列表
* @param page
* @param username
* @param roleId
* @param sex
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map getList(Page page,
@RequestParam(name="username",required=false,defaultValue="") String username,
@RequestParam(name="roleId",required=false) Long roleId,
@RequestParam(name="sex",required=false) Integer sex
){
Map ret = new HashMap();
Map queryMap = new HashMap();
queryMap.put("username", username);
queryMap.put("roleId", roleId);
queryMap.put("sex", sex);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", userService.findList(queryMap));
ret.put("total", userService.getTotal(queryMap));
return ret;
}
/**
* 添加用户
* @param user
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map add(User user){
Map ret = new HashMap();
if(user == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的用户信息!");
return ret;
}
if(StringUtils.isEmpty(user.getUsername())){
ret.put("type", "error");
ret.put("msg", "请填写用户名!");
return ret;
}
if(StringUtils.isEmpty(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "请填写密码!");
return ret;
}
if(user.getRoleId() == null){
ret.put("type", "error");
ret.put("msg", "请选择所属角色!");
return ret;
}
if(isExist(user.getUsername(), 0l)){
ret.put("type", "error");
ret.put("msg", "该用户名已经存在,请重新输入!");
return ret;
}
if(userService.add(user) <= 0){
ret.put("type", "error");
ret.put("msg", "用户添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "角色添加成功!");
return ret;
}
/**
* 编辑用户
* @param user
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map edit(User user){
Map ret = new HashMap();
if(user == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的用户信息!");
return ret;
}
if(StringUtils.isEmpty(user.getUsername())){
ret.put("type", "error");
ret.put("msg", "请填写用户名!");
return ret;
}
// if(StringUtils.isEmpty(user.getPassword())){
// ret.put("type", "error");
// ret.put("msg", "请填写密码!");
// return ret;
// }
if(user.getRoleId() == null){
ret.put("type", "error");
ret.put("msg", "请选择所属角色!");
return ret;
}
if(isExist(user.getUsername(), user.getId())){
ret.put("type", "error");
ret.put("msg", "该用户名已经存在,请重新输入!");
return ret;
}
if(userService.edit(user) <= 0){
ret.put("type", "error");
ret.put("msg", "用户添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "角色添加成功!");
return ret;
}
/**
* 批量删除用户
* @param ids
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Map delete(String ids){
Map ret = new HashMap();
if(StringUtils.isEmpty(ids)){
ret.put("type", "error");
ret.put("msg", "选择要删除的数据!");
return ret;
}
if(ids.contains(",")){
ids = ids.substring(0,ids.length()-1);
}
if(userService.delete(ids) <= 0){
ret.put("type", "error");
ret.put("msg", "用户删除失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "用户删除成功!");
return ret;
}
/**
* 上传图片
* @param photo
* @param request
* @return
*/
@RequestMapping(value="/upload_photo",method=RequestMethod.POST)
@ResponseBody
public Map uploadPhoto(MultipartFile photo,HttpServletRequest request){
Map ret = new HashMap();
if(photo == null){
ret.put("type", "error");
ret.put("msg", "选择要上传的文件!");
return ret;
}
if(photo.getSize() > 1024*1024*1024){
ret.put("type", "error");
ret.put("msg", "文件大小不能超过10M!");
return ret;
}
//获取文件后缀
String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf(".")+1,photo.getOriginalFilename().length());
if(!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())){
ret.put("type", "error");
ret.put("msg", "请选择jpg,jpeg,gif,png格式的图片!");
return ret;
}
String savePath = request.getServletContext().getRealPath("/") + "/resources/upload/";
File savePathFile = new File(savePath);
if(!savePathFile.exists()){
//若不存在改目录,则创建目录
savePathFile.mkdir();
}
String filename = new Date().getTime()+"."+suffix;
try {
//将文件保存至指定目录
photo.transferTo(new File(savePath+filename));
}catch (Exception e) {
// TODO Auto-generated catch block
ret.put("type", "error");
ret.put("msg", "保存文件异常!");
e.printStackTrace();
return ret;
}
ret.put("type", "success");
ret.put("msg", "用户成功!");
ret.put("filepath",request.getServletContext().getContextPath() + "/resources/upload/" + filename );
return ret;
}
/**
* 判断该用户名是否存在
* @param username
* @param id
* @return
*/
private boolean isExist(String username,Long id){
User user = userService.findByUsername(username);
if(user == null)return false;
if(user.getId().longValue() == id.longValue())return false;
return true;
}
@RequestMapping("/admin/log")
@Controller
public class LogController {
@Autowired
private LogService logService;
/**
* 日志列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.setViewName("log/list");
return model;
}
/**
* 获取日志列表
* @param page
* @param content
* @param roleId
* @param sex
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map getList(Page page,
//@RequestParam(name="content",required=false,defaultValue="")
String content
){
Map ret = new HashMap();
Map queryMap = new HashMap();
queryMap.put("content", content);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", logService.findList(queryMap));
ret.put("total", logService.getTotal(queryMap));
return ret;
}
/**
* 添加日志
* @param user
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map add(Log log){
Map ret = new HashMap();
if(log == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的日志信息!");
return ret;
}
if(StringUtils.isEmpty(log.getContent())){
ret.put("type", "error");
ret.put("msg", "请填写日志内容!");
return ret;
}
log.setCreateTime(new Date());
if(logService.add(log) <= 0){
ret.put("type", "error");
ret.put("msg", "日志添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "日志添加成功!");
return ret;
}
/**
* 批量删除日志
* @param ids
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Map delete(String ids){
Map ret = new HashMap();
if(StringUtils.isEmpty(ids)){
ret.put("type", "error");
ret.put("msg", "选择要删除的数据!");
return ret;
}
if(ids.contains(",")){
ids = ids.substring(0,ids.length()-1);
}
if(logService.delete(ids) <= 0){
ret.put("type", "error");
ret.put("msg", "日志删除失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "日志删除成功!");
return ret;
}
}