diff --git a/project/src/main/java/cyou/chenx221/controller/AdminController.java b/project/src/main/java/cyou/chenx221/controller/AdminController.java index bd75ff0..07eefe5 100644 --- a/project/src/main/java/cyou/chenx221/controller/AdminController.java +++ b/project/src/main/java/cyou/chenx221/controller/AdminController.java @@ -1,44 +1,53 @@ package cyou.chenx221.controller; - +import cyou.chenx221.modal.Admin; import cyou.chenx221.service.AdminService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; - +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +/** + * 用户登录和注销Controller + */ @Controller public class AdminController { - + @RequestMapping("/toMainPage") + public String toMainPage(){ + return "main"; + } + //注入userService @Autowired private AdminService adminService; - + /* + 用户登录 + */ @RequestMapping("/login") - public String login(@RequestParam("username") String username, - @RequestParam("password") String password) { - - // 进行用户名和密码的验证逻辑 - if (adminService.validateCredentials(username, password)) { - // 验证成功,进行相应的操作 - // 这里可以进行重定向或返回视图等操作 - return "redirect:/admin/dashboard"; - } else { - // 验证失败,返回登录页面或错误提示 - return "redirect:/admin/login?error"; + public String login(Admin admin, HttpServletRequest request){ + try { + Admin a = adminService.login(admin); + if(a!=null){ + request.getSession().setAttribute("USER_SESSION",a); + return "redirect:/main.jsp"; + } + request.setAttribute("msg","用户名或密码错误"); + return "forward:/login.jsp"; + }catch(Exception e){ + e.printStackTrace(); + request.setAttribute("msg","系统错误"); + return "forward:/login.jsp"; } } - @GetMapping("/dashboard") - public String dashboard() { - // 处理管理员登录后的仪表盘页面 - // 返回相应的视图页面 - return "admin/dashboard"; - } - - @GetMapping("/logout") - public String logout() { - // 执行登出操作,清除用户信息或会话信息 - return "redirect:/admin/login"; + @RequestMapping("/logout") + public String logout( HttpServletRequest request){ + try { + HttpSession session = request.getSession(); + session.invalidate(); + return "forward:/login.jsp"; + }catch(Exception e){ + e.printStackTrace(); + request.setAttribute("msg","系统错误"); + return "forward:/login.jsp"; + } } } diff --git a/project/src/main/java/cyou/chenx221/mapper/AdminMapper.java b/project/src/main/java/cyou/chenx221/mapper/AdminMapper.java index 8089c64..7186b68 100644 --- a/project/src/main/java/cyou/chenx221/mapper/AdminMapper.java +++ b/project/src/main/java/cyou/chenx221/mapper/AdminMapper.java @@ -1,13 +1,17 @@ package cyou.chenx221.mapper; import cyou.chenx221.modal.Admin; +import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; +import org.springframework.stereotype.Repository; +@Mapper +@Repository public interface AdminMapper { @Select("select * from user where username=#{username} AND password=#{password}") - @Results(id = "userMap",value = { + @Results(id = "adminMap",value = { @Result(id = true,column = "user_id",property = "id"), @Result(column = "username",property = "name"), @Result(column = "password",property = "password"), diff --git a/project/src/main/java/cyou/chenx221/service/impl/AdminServiceImpl.java b/project/src/main/java/cyou/chenx221/service/impl/AdminServiceImpl.java index bbb8e56..369ef4a 100644 --- a/project/src/main/java/cyou/chenx221/service/impl/AdminServiceImpl.java +++ b/project/src/main/java/cyou/chenx221/service/impl/AdminServiceImpl.java @@ -1,6 +1,18 @@ package cyou.chenx221.service.impl; +import cyou.chenx221.mapper.AdminMapper; +import cyou.chenx221.modal.Admin; +import cyou.chenx221.service.AdminService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + @Service public class AdminServiceImpl implements AdminService { + @Autowired + private AdminMapper adminMapper; + @Override + public Admin login(Admin admin) { + return null; + } } diff --git a/project/web/login.jsp b/project/web/login2.jsp similarity index 99% rename from project/web/login.jsp rename to project/web/login2.jsp index 4a71306..f810697 100644 --- a/project/web/login.jsp +++ b/project/web/login2.jsp @@ -55,7 +55,7 @@