SSM 电影后台管理项目 (6)

因为代码过长,在此只举例User、types的Controller层代码

@Controller public class UserController { @Autowired private HttpServletRequest request; @Autowired @Qualifier("userServiceimpl") private UserService userService; /** * 登录 * @param username * @param password * @param code * @return */ @RequestMapping("/Login") public String getUserList(String username, String password, String code){ List<user> userList = userService.getUserList(); for (user user : userList) { System.out.println(user); if(user.getUsername().equals(username)&&user.getPaw().equals(password)){ HttpSession session = request.getSession(); Object attribute = session.getAttribute(Constants.KAPTCHA_SESSION_KEY); if(code.equals(attribute)){ session.setAttribute("user",user); return "main.jsp"; }else { request.setAttribute("mgs", "验证码错误"); return "index.jsp"; } } } // System.out.println(user.getUsername()+"-----"+user.getPaw()); request.setAttribute("mgs", "用户名或密码错误"); return "index.jsp"; } /** * 注销 * @return */ @RequestMapping("/exit") public String exit(){ request.getSession().removeAttribute("user"); return "index.jsp"; } /** * 注册 * @param user * @return */ @RequestMapping("/register") public String insertUser(user user){ List<user> userList = userService.getUserList(); for (user user1 : userList) { if (user1.getUsername().equals(user.getUsername())){ request.setAttribute("mgs1", "已经存在该用户"); return "index.jsp"; }else { System.out.println(user); userService.insertUser(user); return "index.jsp"; } } return "index.jsp"; } /** * 修改用户信息 * @param user * @return */ @RequestMapping("/upUser") public String upUser(user user){ int i = userService.upUser(user); System.out.println(user+"-----"+i); if (i>0){ user user1 = userService.getUserById(user.getId()); request.getSession().setAttribute("user",user1); request.setAttribute("mgs4","修改成功"); return "person/person_info.jsp"; }else{ request.setAttribute("mgs4","修改失败"); return "person/person_info.jsp"; } } /** * 修改密码 * @param user * @return */ @RequestMapping("/uppaw") public String uppaw(user user, String paw1){ user userById = userService.getUserById(user.getId()); System.out.println(user+"----------"+paw1); if(userById.getPaw().equals(paw1)){ userService.uppaw(user); user user1 = userService.getUserById(user.getId()); request.getSession().setAttribute("user",user1); request.setAttribute("mgs3","修改密码成功"); return "person/updatepwd.jsp"; }else{ request.setAttribute("mgs3","输入原始密码不对"); return "person/updatepwd.jsp"; } } } @Controller public class TypesController { @Autowired private HttpServletRequest request; @Autowired @Qualifier("typesServiceimpl") private TypesService typesService; /** * 查找全部种类 * @param model * @return */ @RequestMapping("/alltypes") public String Alltypes(Model model){ List<types> alltypes = typesService.Alltypes(); model.addAttribute("alltypes",alltypes); return "film/File_cate.jsp"; } /** * 添加种类 * @param types * @return */ @RequestMapping("/addtype") public String addtype(types types){ System.out.println(types); List<types> alltypes = typesService.Alltypes(); for (com.zc.pojo.types alltype : alltypes) { if (alltype.getId().equals(types.getId())){ request.setAttribute("addtype","已经存在该电影类别编号"); return "forward:/alltypes"; } else if (alltype.getType().equals(types.getType())){ request.setAttribute("addtype","已经存在该电影类别"); return "forward:/alltypes"; } } int i = typesService.addtype(types); if(i>0){ request.setAttribute("addtype","添加成功"); return "forward:/alltypes"; }else{ request.setAttribute("addtype","添加失败"); return "forward:/alltypes"; } } /** * 更新种类 * @param types * @return */ @RequestMapping("/uptype") public String Updatetype(types types){ List<types> alltypes = typesService.Alltypes(); for (com.zc.pojo.types alltype : alltypes) { if (alltype.getType().equals(types.getType())) { request.setAttribute("addtype", "已经存在该电影类别"); return "forward:/alltypes"; } else { int i = typesService.Updatetype(types); if(i>0){ request.setAttribute("addtype","添加成功"); return "forward:/alltypes"; }else{ request.setAttribute("addtype","添加失败"); return "forward:/alltypes"; } } } //System.out.println(types); return "forward:/alltypes"; } /** * 删除种类 * @param id * @return */ @RequestMapping("/deltype") public String Deltype(int id){ int i = typesService.DeletetypeById(id); if(i>0){ request.setAttribute("addtype","删除成功"); return "forward:/alltypes"; }else{ request.setAttribute("addtype","删除失败"); return "forward:/alltypes"; } } } 9.过滤器、拦截器 9.1 过滤器 public class LoginFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String servletPath = request.getServletPath(); //获取客户端所请求的脚本文件的文件路径 if(servletPath.equals("/index.jsp") ||servletPath.equals("/Login")||servletPath.equals(".js")||servletPath.equals(".css")||servletPath.equals(".png")) { filterChain.doFilter(request,response); }else { HttpSession session = request.getSession(); if (session.getAttribute("user") == null) { // 没有登录 response.sendRedirect(request.getContextPath() + "/index.jsp"); }else { filterChain.doFilter(request,response); } } } @Override public void destroy() { } } 9.2 拦截器 public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException, ServletException { if (request.getRequestURI().contains(".js")||request.getRequestURI().contains(".css") ||request.getRequestURI().contains(".png")||request.getRequestURI().contains(".jpg")) { return true; } HttpSession session = request.getSession(); if (session.getAttribute("user")!= null) { return true; } request.getRequestDispatcher(request.getContextPath()+"/index.jsp").forward(request,response); return false; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspxxj.html