5、Entity层(User实体类)
package cn.kgc.entity; /** * @author:BeiisBei * @date:2019/11/8 * @aim: */ public class User { private String name; private String pwd; public User(String name, String pwd) { this.name = name; this.pwd = pwd; } public User() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
6、改造Controller层
创建LoginServlet,实现以下业务功能:
跳转到系统登录页
实现登录/登出
改造View层
具体实现步骤如下:
1、跳转到系统登录页,login()方法实现跳转。
2、实现登录,doLogin()方法实现登录,登录成功则跳转到系统首页,否则跳转到登录页面。doLogin()方法主要进行用户信息匹配验证。
首先,通过方法入参(@RequestParam String name, @RequestParam String pwd) 来获取用户前台输入。
然后调用业务方法,将参数(name,pwd)传入后台,进行用户匹配,根据返回结果来判断是否登录成功。
package cn.kgc.servlet; import cn.kgc.entity.User; import cn.kgc.service.UserService; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * @author:BeiisBei * @date:2019/11/8 * @aim: */ @Controller @RequestMapping("/login") public class LoginServlet { private Logger logger=Logger.getLogger(LoginServlet.class); @Resource private UserService userService; @RequestMapping("/login") public String login(String name,String pwd){ logger.info("欢迎登录"); return "login"; } /** * 登录 * @param name * @param pwd * @param request * @param session * @return */ @RequestMapping("/doLogin") public ModelAndView doLogin(@RequestParam String name, @RequestParam String pwd, HttpServletRequest request, HttpSession session){ //调用service方法,进行用户匹配 User user=userService.login(name,pwd); ModelAndView mav=new ModelAndView(); logger.info(request.getParameter("name")); logger.info(user); if(null==user.getName()){ // 登录不成功 logger.info("用户名或密码不正确"); session.setAttribute("errMsg","用户名或密码不正确"); mav.setViewName("login"); throw new RuntimeException("用户名或密码不正确"); }else { logger.info("登录成功"); session.setAttribute("user",user); mav.addObject("user",user); mav.setViewName("redirect:../jsp/loginSuc.jsp"); } return mav; } /** * 登出 * @param name * @param session * @return */ @RequestMapping("/logout") public String logout(@RequestParam String name,HttpSession session){ session.invalidate(); return "redirect:login"; } /** * 局部异常 * @param session * @param e * @param request * @return */ @ExceptionHandler(value = {RuntimeException.class}) public String errHandler(HttpSession session,RuntimeException e,HttpServletRequest request){ session.invalidate(); logger.info(e.getMessage()); request.setAttribute("e",e); return "loginErr"; } }
指示符 —“redirect:” 转发
指示符 —“forward:” 重定向
1.登录成功后的当前用户信息需保存在session中
2.若登录失败,除了页面跳转到登陆页,还需在页面上进行错误提示
3.Servlet API对象作为处理方法的入参
1.HttpSession
2.HttpServletRequest
4.页面上使用EL表达式显示相应信息
异常处理
HandlerExceptionResolver
resolveException()
局部异常处理
仅能处理指定Controller中的异常
@ExceptionHandler
全局异常处理
对所有异常进行统一处理
配置SimpleMappingExceptionResolver
发生异常时使用对应的视图报告异常
7、改造View层
1、login.jsp
<%-- Created by IntelliJ IDEA. User: mu_bai Date: 2019/11/8 Time: 14:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录界面</title> </head> <body> <form action="/login/doLogin" method="post"> <p><input type="text" name="name"/></p> <p><input type="password" name="pwd"/></p> <p><input type="submit"/></p> </form> </body> </html>
2、loginErr.jsp
<%-- Created by IntelliJ IDEA. User: mu_bai Date: 2019/11/8 Time: 14:23 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登入失败页面</title> </head> <body> <h1>登入失败</h1> <h1>${e.message}</h1> <%--下面这个方法,errMsg在页面上获取不到,显示为NULL,标记下,解决了再更新代码--%> <%--<% Object obj=request.getParameter("e"); RuntimeException errMsg=null; if(obj!=null){ errMsg=(RuntimeException) obj; } %> <h1><%=errMsg%></h1>--%> </body> </html>
3、loginSuc.jsp
<%@ page import="cn.kgc.entity.User" %><%-- Created by IntelliJ IDEA. User: mu_bai Date: 2019/11/8 Time: 14:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登入成功页面</title> </head> <% Object obj=session.getAttribute("user"); User user=null; String name=null; if(obj!=null){ user=(User)obj; name=user.getName(); }else { response.sendRedirect("/login/login"); } %> <body> <h1>登录成功了</h1> <h3><%=name==null? "" : "欢迎你:"+name %></h3> <form method="post" action="/login/logout"> <input type="text" name="name" value="<%=name==null? "": name %>" readonly> <input type="submit" value="登出"> </form> </body> </html>
8、结果演示
1、登录界面
2、登录成功界面(点击登出,会退回到登录界面)
3、登录失败界面(会打印出我们设置的异常)