本文主要是介绍基于javaweb的嘟嘟校园一卡通系统(java+jsp+servlet+html+css+javascript+jquery+ajax+mysql),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于javaweb的嘟嘟校园一卡通系统(java+jsp+servlet+html+css+javascript+jquery+ajax+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+jsp的校园一卡通系统(java+JSP+Servlet+html+css+JavaScript+JQuery+Ajax+mysql)
一、项目简述
功能:卡管理,卡消费,卡充值,图书借阅,消费,记录,注销等等功能。
二、项目运行
环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
JSP + Servlet + html+ css + JavaScript + JQuery + Ajax 等等
用户管理操作控制层:
/**
- 用户管理操作
*/
@Controller
@RequestMapping(“/user”)
public class UserController {
@Autowired
private UserService userService;
/**
-
用户添加页面
-
@return
*/
@GetMapping(“/add”)
public String create() {
return “user/add”;
/**
-
用户添加操作
-
@param user
-
@return
*/
@PostMapping(“/add”)
@ResponseBody
public Map<String, Object> add(@RequestBody User user) {
if(StringUtils.isEmpty(user.getUserName())){
return MapControl.getInstance().error(“请填写用户名”).getMap();
if(StringUtils.isEmpty(user.getName())){
return MapControl.getInstance().error(“请填写名称”).getMap();
if(StringUtils.isEmpty(user.getUserPwd())){
return MapControl.getInstance().error(“请填写密码”).getMap();
int result = userService.create(user);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
return MapControl.getInstance().success().getMap();
/**
-
根据id删除
-
@param id
-
@return
*/
@PostMapping(“/delete/{id}”)
@ResponseBody
public Map<String, Object> delete(@PathVariable(“id”) Integer id) {
int result = userService.delete(id);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
return MapControl.getInstance().success().getMap();
//批量删除
@PostMapping(“/delete”)
@ResponseBody
public Map<String, Object> delete(String ids) {
int result = userService.delete(ids);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
return MapControl.getInstance().success().getMap();
/**
-
编辑用户信息操作
-
@param user
-
@return
*/
@PostMapping(“/edit”)
@ResponseBody
public Map<String, Object> edit(@RequestBody User user) {
if(StringUtils.isEmpty(user.getUserName())){
return MapControl.getInstance().error(“请填写用户名”).getMap();
if(StringUtils.isEmpty(user.getName())){
return MapControl.getInstance().error(“请填写名称”).getMap();
if(StringUtils.isEmpty(user.getUserPwd())){
return MapControl.getInstance().error(“请填写密码”).getMap();
int result = userService.update(user);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
return MapControl.getInstance().success().getMap();
/**
-
根据id查询,跳转修改页面
-
@param id
-
@param modelMap
-
@return
*/
@GetMapping(“/edit/{id}”)
public String edit(@PathVariable(“id”) Integer id, ModelMap modelMap) {
User user = userService.detail(id);
modelMap.addAttribute(“user”, user);
return “user/edit”;
//查询所有
@PostMapping(“/query”)
@ResponseBody
public Map<String, Object> query(@RequestBody User user) {
List list = userService.query(user);
Integer count = userService.count(user);
return MapControl.getInstance().success().page(list, count).getMap();
//跳转列表页面
@GetMapping(“/list”)
public String list() {
return “user/list”;
登录控制层:
@Controller
public class LoginController {
@Autowired
private UserService userService;
@Autowired
private TeacherService teacherService;
@Autowired
private StudentService studentService;
//跳转登录页面
@GetMapping(“/login”)
public String login() {
return “login”;
//登录操作
@PostMapping(“/login”)
@ResponseBody
public Map<String, Object> login(String userName, String password, String captcha, String type, HttpSession session) {
//判断用户名、密码、用户类型、验证码是否为空
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password) || StringUtils.isEmpty(captcha) || StringUtils.isEmpty(type)) {
return MapControl.getInstance().error(“用户名或密码不能为空”).getMap();
//获取系统生成的验证码
String _captcha = (String) session.getAttribute(“captcha”);
//先判断验证码是否正确
if (!(captcha.toLowerCase()).equals(_captcha.toLowerCase())) {
//验证码错误
return MapControl.getInstance().error(“验证码错误”).getMap();
//判断用户类型
if (“1”.equals(type)) { //管理员验证登录
User user = userService.login(userName, MD5Utils.getMD5(password)); //对密码进行加密处理,因为数据库中存储的是加密后的密码
if (user != null) {
session.setAttribute(“user”, user);
session.setAttribute(“type”, 1);
return MapControl.getInstance().success().add(“data”, user).getMap();
} else {
return MapControl.getInstance().error(“用户名或密码错误”).getMap();
if (“2”.equals(type)) { //老师验证登录
Teacher teacher = teacherService.login(userName, MD5Utils.getMD5(password));
if (teacher != null) {
session.setAttribute(“user”, teacher);
session.setAttribute(“type”, “2”);
return MapControl.getInstance().success().add(“data”, teacher).getMap();
} else {
return MapControl.getInstance().error(“用户名或密码错误”).getMap();
if (“3”.equals(type)) { //学生验证登录
Student student = studentService.login(userName, MD5Utils.getMD5(password));
if (student != null) {
session.setAttribute(“user”, student);
session.setAttribute(“type”, “3”);
return MapControl.getInstance().success().add(“data”, student).getMap();
} else {
return MapControl.getInstance().error(“用户名或密码错误”).getMap();
return MapControl.getInstance().getMap();
生成验证码:
@Controller
@RequestMapping(“/captcha”)
public class CaptchaController {
private char[] codeSequence = {‘A’, ‘1’, ‘B’, ‘C’, ‘2’, ‘D’, ‘3’, ‘E’, ‘4’, ‘F’, ‘5’, ‘G’, ‘6’, ‘H’, ‘7’, ‘I’, ‘8’, ‘J’,
‘K’, ‘9’, ‘L’, ‘1’, ‘M’, ‘2’, ‘N’, ‘P’, ‘3’, ‘Q’, ‘4’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’,
‘X’, ‘Y’, ‘Z’};
@RequestMapping(“/code”)
public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
int width = 80;
int height = 37;
Random random = new Random();
//设置response头信息
//禁止缓存
response.setHeader(“Pragma”, “No-cache”);
response.setHeader(“Cache-Control”, “no-cache”);
response.setDateHeader(“Expires”, 0);
//生成缓冲区image类
BufferedImage image = new BufferedImage(width, height, 1);
//产生image类的Graphics用于绘制操作
Graphics g = image.getGraphics();
//Graphics类的样式
g.setColor(this.getColor(200, 250));
g.setFont(new Font(“Times New Roman”, 0, 28));
g.fillRect(0, 0, width, height);
//绘制干扰线
for (int i = 0; i < 40; i++) {
g.setColor(this.getColor(130, 200));
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x, y, x + x1, y + y1);
//绘制字符
String strCode = “”;
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
strCode = strCode + rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 28);
//将字符保存到session中用于前端的验证
session.setAttribute(“captcha”, strCode.toLowerCase());
g.dispose();
ImageIO.write(image, “JPEG”, response.getOutputStream());
response.getOutputStream().flush();
public Color getColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
后台角色管理控制器:
/**
- 后台角色管理控制器
*/
@RequestMapping(“/admin/role”)
@Controller
public class RoleController {
private Logger log = LoggerFactory.getLogger(RoleController.class);
@Autowired
private MenuService menuService;
@Autowired
private OperaterLogService operaterLogService;
@Autowired
private RoleService roleService;
/**
-
分页搜索角色列表
-
@param model
-
@param role
-
@param pageBean
-
@return
*/
@RequestMapping(value=“/list”)
public String list(Model model,Role role,PageBean pageBean){
model.addAttribute(“title”, “角色列表”);
model.addAttribute(“name”, role.getName());
model.addAttribute(“pageBean”, roleService.findByName(role, pageBean));
return “admin/role/list”;
/**
-
角色添加页面
-
@param model
-
@return
*/
@RequestMapping(value=“/add”,method=RequestMethod.GET)
public String add(Model model){
List
model.addAttribute(“topMenus”,MenuUtil.getTopMenus(findAll));
model.addAttribute(“secondMenus”,MenuUtil.getSecondMenus(findAll));
model.addAttribute(“thirdMenus”,MenuUtil.getThirdMenus(findAll));
return “admin/role/add”;
/**
-
角色添加表单提交处理
-
@param role
-
@return
*/
@RequestMapping(value=“/add”,method=RequestMethod.POST)
@ResponseBody
public Result add(Role role){
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(role);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
if(roleService.save(role) == null){
return Result.error(CodeMsg.ADMIN_ROLE_ADD_ERROR);
log.info(“添加角色【”+role+“】”);
operaterLogService.add(“添加角色【”+role.getName()+“】”);
return Result.success(true);
/**
-
角色编辑页面
-
@param id
-
@param model
-
@return
*/
@RequestMapping(value=“/edit”,method=RequestMethod.GET)
public String edit(@RequestParam(name=“id”,required=true)Long id,Model model){
List
model.addAttribute(“topMenus”,MenuUtil.getTopMenus(findAll));
model.addAttribute(“secondMenus”,MenuUtil.getSecondMenus(findAll));
model.addAttribute(“thirdMenus”,MenuUtil.getThirdMenus(findAll));
Role role = roleService.find(id);
model.addAttribute(“role”, role);
model.addAttribute(“authorities”,JSONArray.toJSON(role.getAuthorities()).toString());
return “admin/role/edit”;
/**
-
角色修改表单提交处理
-
@param request
-
@param role
-
@return
*/
@RequestMapping(value=“/edit”,method=RequestMethod.POST)
@ResponseBody
public Result edit(Role role){
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(role);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
Role existRole = roleService.find(role.getId());
if(existRole == null){
return Result.error(CodeMsg.ADMIN_ROLE_NO_EXIST);
existRole.setName(role.getName());
existRole.setRemark(role.getRemark());
existRole.setStatus(role.getStatus());
existRole.setAuthorities(role.getAuthorities());
if(roleService.save(existRole) == null){
return Result.error(CodeMsg.ADMIN_ROLE_EDIT_ERROR);
log.info(“编辑角色【”+role+“】”);
operaterLogService.add(“编辑角色【”+role.getName()+“】”);
return Result.success(true);
/**
-
删除角色
-
@param request
-
@param id
-
@return
*/
@RequestMapping(value=“delete”,method=RequestMethod.POST)
@ResponseBody
public Result delete(@RequestParam(name=“id”,required=true)Long id){
try {
roleService.delete(id);
} catch (Exception e) {
// TODO: handle exception
return Result.error(CodeMsg.ADMIN_ROLE_DELETE_ERROR);
log.info(“编辑角色ID【”+id+“】”);
operaterLogService.add(“删除角色ID【”+id+“】”);
return Result.success(true);
系统控制器:
/**
- 系统控制器
*/
@RequestMapping(“/system”)
@Controller
public class SystemController {
@Autowired
private OperaterLogService operaterLogService;
@Autowired
private UserService userService;
@Autowired
private DatabaseBakService databaseBakService;
@Autowired
private StaffService staffService;
@Autowired
private OrderAuthService orderAuthService;
private Logger log = LoggerFactory.getLogger(SystemController.class);
/**
-
登录页面
-
@param model
-
@return
*/
@RequestMapping(value=“/login”,method=RequestMethod.GET)
public String login(Model model){
model.addAttribute(“loginTypes”, LoginType.values());
return “admin/system/login”;
/**
-
用户登录提交表单处理方法
-
@param request
-
@param cpacha
-
@return
*/
@RequestMapping(value=“/login”,method=RequestMethod.POST)
@ResponseBody
public Result login(HttpServletRequest request,String username,String password,String cpacha,Integer type){
if(StringUtils.isEmpty(username)){
return Result.error(CodeMsg.ADMIN_USERNAME_EMPTY);
if(StringUtils.isEmpty(password)){
return Result.error(CodeMsg.ADMIN_PASSWORD_EMPTY);
//表示实体信息合法,开始验证验证码是否为空
if(StringUtils.isEmpty(cpacha)){
return Result.error(CodeMsg.CPACHA_EMPTY);
//说明验证码不为空,从session里获取验证码
Object attribute = request.getSession().getAttribute(“admin_login”);
if(attribute == null){
return Result.error(CodeMsg.SESSION_EXPIRED);
//表示session未失效,进一步判断用户填写的验证码是否正确
if(!cpacha.equalsIgnoreCase(attribute.toString())){
return Result.error(CodeMsg.CPACHA_ERROR);
if(type == LoginType.ADMINISTRATOR.getCode()){
//表示验证码正确,开始查询数据库,检验密码是否正确
User findByUsername = userService.findByUsername(username);
//判断是否为空
if(findByUsername == null){
return Result.error(CodeMsg.ADMIN_USERNAME_NO_EXIST);
//表示用户存在,进一步对比密码是否正确
if(!findByUsername.getPassword().equals(password)){
return Result.error(CodeMsg.ADMIN_PASSWORD_ERROR);
//表示密码正确,接下来判断用户状态是否可用
if(findByUsername.getStatus() == User.ADMIN_USER_STATUS_UNABLE){
return Result.error(CodeMsg.ADMIN_USER_UNABLE);
//检查用户所属角色状态是否可用
if(findByUsername.getRole() == null || findByUsername.getRole().getStatus() == Role.ADMIN_ROLE_STATUS_UNABLE){
return Result.error(CodeMsg.ADMIN_USER_ROLE_UNABLE);
//检查用户所属角色的权限是否存在
if(findByUsername.getRole().getAuthorities() == null || findByUsername.getRole().getAuthorities().size() == 0){
return Result.error(CodeMsg.ADMIN_USER_ROLE_AUTHORITES_EMPTY);
//检查一切符合,可以登录,将用户信息存放至session
request.getSession().setAttribute(SessionConstant.SESSION_USER_LOGIN_KEY, findByUsername);
request.getSession().setAttribute(“loginType”,type);
//销毁session中的验证码
request.getSession().setAttribute(“admin_login”, null);
//将登陆记录写入日志库
operaterLogService.add(“用户【”+username+“】于【” + StringUtil.getFormatterDate(new Date(), “yyyy-MM-dd HH:mm:ss”) + “】登录系统!”);
log.info("用户成功登录,user = " + findByUsername);
}else{
Staff byJobNumber = staffService.findByNameAndIsStatus(username);
//判断是否为空
if(byJobNumber == null){
return Result.error(CodeMsg.ADMIN_USERNAME_NO_EXIST);
//表示用户存在,进一步对比密码是否正确
if(!byJobNumber.getPassword().equals(password)){
return Result.error(CodeMsg.ADMIN_PASSWORD_ERROR);
//检查用户所属角色状态是否可用
if(byJobNumber.getRole() == null || byJobNumber.getRole().getStatus() == Role.ADMIN_ROLE_STATUS_UNABLE){
return Result.error(CodeMsg.ADMIN_USER_ROLE_UNABLE);
//检查用户所属角色的权限是否存在
if(byJobNumber.getRole().getAuthorities() == null || byJobNumber.getRole().getAuthorities().size() == 0){
return Result.error(CodeMsg.ADMIN_USER_ROLE_AUTHORITES_EMPTY);
//检查一切符合,可以登录,将用户信息存放至session
request.getSession().setAttribute(SessionConstant.SESSION_STAFF_LOGIN_KEY, byJobNumber);
request.getSession().setAttribute(“loginType”,type);
//销毁session中的验证码
request.getSession().setAttribute(“admin_login”, null);
//将登陆记录写入日志库
operaterLogService.add(“用户【”+username+“】于【” + StringUtil.getFormatterDate(new Date(), “yyyy-MM-dd HH:mm:ss”) + “】登录系统!”);
log.info("员工成功登录,user = " + byJobNumber);
return Result.success(true);
/**
-
登录成功后的系统主页
-
@param model
-
@return
*/
@RequestMapping(value=“/index”)
public String index(Model model){
model.addAttribute(“operatorLogs”, operaterLogService.findLastestLog(10));
model.addAttribute(“userTotal”, userService.total());
model.addAttribute(“operatorLogTotal”, operaterLogService.total());
model.addAttribute(“databaseBackupTotal”, databaseBakService.total());
model.addAttribute(“onlineUserTotal”, SessionListener.onlineUserCount);
return “admin/system/index”;
/**
-
注销登录
-
@return
*/
@RequestMapping(value=“/logout”)
public String logout(){
Integer loginType = (Integer) SessionUtil.get(“loginType”);
if(loginType == LoginType.ADMINISTRATOR.getCode()){
User loginedUser = SessionUtil.getLoginedUser();
if(loginedUser != null){
SessionUtil.set(SessionConstant.SESSION_USER_LOGIN_KEY, null);
}else if(loginType == LoginType.STAFF.getCode()){
Staff loginedStaff = SessionUtil.getLoginedStaff();
if(loginedStaff != null){
SessionUtil.set(SessionConstant.SESSION_STAFF_LOGIN_KEY,null);
return “redirect:login”;
/**
-
无权限提示页面
-
@return
*/
@RequestMapping(value=“/no_right”)
public String noRight(){
return “admin/system/no_right”;
/**
-
修改用户个人信息
-
@return
*/
@RequestMapping(value=“/update_userinfo”,method=RequestMethod.GET)
public String updateUserInfo(){
return “admin/system/update_userinfo”;
/**
-
修改个人信息保存
-
@param user
-
@return
*/
@RequestMapping(value=“/update_userinfo”,method=RequestMethod.POST)
public String updateUserInfo(User user){
User loginedUser = SessionUtil.getLoginedUser();
loginedUser.setEmail(user.getEmail());
loginedUser.setMobile(user.getMobile());
loginedUser.setHeadPic(user.getHeadPic());
//首先保存到数据库
userService.save(loginedUser);
//更新session里的值
SessionUtil.set(SessionConstant.SESSION_USER_LOGIN_KEY, loginedUser);
return “redirect:update_userinfo”;
/**
-
修改密码页面
-
@return
*/
@RequestMapping(value=“/update_pwd”,method=RequestMethod.GET)
public String updatePwd(){
return “admin/system/update_pwd”;
/**
-
修改密码表单提交
-
@param oldPwd
-
@param newPwd
-
@return
*/
@RequestMapping(value=“/update_pwd”,method=RequestMethod.POST)
@ResponseBody
public Result updatePwd(@RequestParam(name=“oldPwd”,required=true)String oldPwd,
@RequestParam(name=“newPwd”,required=true)String newPwd
){
Integer loginType = (Integer) SessionUtil.get(“loginType”);
if(loginType == LoginType.ADMINISTRATOR.getCode()){
User loginedUser = SessionUtil.getLoginedUser();
if(!loginedUser.getPassword().equals(oldPwd)){
return Result.error(CodeMsg.ADMIN_USER_UPDATE_PWD_ERROR);
if(StringUtils.isEmpty(newPwd)){
return Result.error(CodeMsg.ADMIN_USER_UPDATE_PWD_EMPTY);
if(newPwd.length()<4 || newPwd.length()>32){
return Result.error(CodeMsg.ADMIN_USER_PWD_LENGTH_ERROR);
loginedUser.setPassword(newPwd);
//保存数据库
userService.save(loginedUser);
//更新session
SessionUtil.set(SessionConstant.SESSION_USER_LOGIN_KEY, loginedUser);
}else{
Staff loginedStaff = SessionUtil.getLoginedStaff();
Staff staff = staffService.find(loginedStaff.getId());
if(!staff.getPassword().equals(oldPwd)){
return Result.error(CodeMsg.ADMIN_USER_UPDATE_PWD_ERROR);
if(StringUtils.isEmpty(newPwd)){
return Result.error(CodeMsg.ADMIN_USER_UPDATE_PWD_EMPTY);
staff.setPassword(newPwd);
CodeMsg codeMsg = ValidateEntityUtil.validate(staff);
if (codeMsg.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(codeMsg);
loginedStaff.setPassword(newPwd);
//保存数据库
staffService.save(loginedStaff);
//更新session
SessionUtil.set(SessionConstant.SESSION_STAFF_LOGIN_KEY, loginedStaff);
return Result.success(true);
/**
-
日志管理列表
-
@param model
-
@param operaterLog
-
@param pageBean
-
@return
*/
@RequestMapping(value=“/operator_log_list”)
public String operatorLogList(Model model,OperaterLog operaterLog,PageBean pageBean){
model.addAttribute(“pageBean”, operaterLogService.findList(operaterLog, pageBean));
model.addAttribute(“operator”, operaterLog.getOperator());
model.addAttribute(“title”, “日志列表”);
return “admin/system/operator_log_list”;
/**
-
验证订单
-
@param orderSn
-
@param phone
-
@return
*/
@RequestMapping(value=“/auth_order”,method=RequestMethod.POST)
@ResponseBody
public Result authOrder(@RequestParam(name=“orderSn”,required=true)String orderSn,@RequestParam(name=“phone”,required=true)String phone){
OrderAuth orderAuth = new OrderAuth();
orderAuth.setMac(StringUtil.getMac());
orderAuth.setOrderSn(orderSn);
orderAuth.setPhone(phone);
orderAuthService.save(orderAuth);
AppConfig.ORDER_AUTH = 1;
return Result.success(true);
/**
-
删除操作日志,可删除多个
-
@param ids
-
@return
*/
@RequestMapping(value=“/delete_operator_log”,method=RequestMethod.POST)
@ResponseBody
public Result delete(String ids){
if(!StringUtils.isEmpty(ids)){
String[] splitIds = ids.split(“,”);
for(String id : splitIds){
operaterLogService.delete(Long.valueOf(id));
return Result.success(true);
/**
-
清空整个日志
-
@return
*/
@RequestMapping(value=“/delete_all_operator_log”,method=RequestMethod.POST)
@ResponseBody
public Result deleteAll(){
operaterLogService.deleteAll();
return Result.success(true);
这篇关于基于javaweb的嘟嘟校园一卡通系统(java+jsp+servlet+html+css+javascript+jquery+ajax+mysql)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!