项目地址:https://github.com/724888/lightnote_new
由于controller中配置了登录时的跳转页面,导致登录失败时会跳到当前页,若用 return "home";跳转到注册页则会导致url出现问题,第二次登录时会出现404错误,于是我新建了错误显示页,并在该页面重定向至登录页面,防止上述错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class UserController { @Resource private UserService userService; @RequestMapping (value= "/login" ) public String login( @Param ( "username" ) String username, @Param ( "password" ) String password,HttpServletRequest request) { User user=userService.checkLogin(username, password); if (user!= null ){ HttpSession session=request.getSession(); session.setAttribute( "user" ,user); return "redirect:/user/home" ; // 路径 WEB-INF/pages/welcome.jsp } request.setAttribute( "error" , "用户名或密码错误" ); return "error" ; } @RequestMapping ( "registerpage" ) public String registerpage() { return "register" ; } @RequestMapping ( "home" ) public String home() { return "home" ; } @RequestMapping ( "register" ) public String register(User user,HttpServletRequest requsest) { if (userService.checkRegisterUsername(user.getUsername())) { return "home" ; } return "error" ; }} |
error.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <%@ page contentType="text/html;charset=UTF-8" language="java" %><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">< html >< head >< script type="text/javascript">setTimeout("javascript:location.href='http://localhost:8080/lightnote/index.jsp'", 3000);</ script ></ head >< body >< div align="center"> < br /> < br /> < br /> < h4 >${error}</ h4 > < h4 >< a href="http://localhost:8080/lightnote/index.jsp" >立即跳转</ a ></ h4 ></ div > </ body ></ html > |
页面较为简陋,可以实现定时跳转和立即跳转,并显示错误原因,重新定向至登录页面。
版权声明:本文为liutong123987原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。