public class loginController { @Autowired private LoginService ls; @RequestMapping("login") public String login(User user,HttpServletRequest request,HttpSession session){ User u = null; u = ls.login(user); if(u!=null){ session.setAttribute("admin", u); return "redirect:/pages/admin/system.jsp"; }else{ request.setAttribute("msg", "用户名或密码错误!"); return "forward:/pages/login.jsp"; } } @RequestMapping("loginout") private String loginout(HttpSession session){ session.removeAttribute("admin"); return "forward:/pages/login.jsp"; } |
package com.mac.controller.admin; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.mac.entity.User; import com.mac.service.LoginService; @Controller public class loginController { @Autowired private LoginService ls; @RequestMapping("login") public String login(User user,HttpServletRequest request,HttpSession session){ User u = null; u = ls.login(user); if(u!=null){ session.setAttribute("admin", u); return "redirect:/pages/admin/system.jsp"; }else{ request.setAttribute("msg", "用户名或密码错误!"); return "forward:/pages/login.jsp"; } } @RequestMapping("loginout") private String loginout(HttpSession session){ session.removeAttribute("admin"); return "forward:/pages/login.jsp"; } } |
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.mac.filters.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/pages/admin/*</url-pattern> </filter-mapping> |