ssm中登录功能的实现以及页面跳转
由于controller中配置了登录时的跳转页面,导致登录失败时会跳到当前页,若用 return "home";跳转到注册页则会导致url出现问题,第二次登录时会出现404错误,于是我新建了错误显示页,并在该页面重定向至登录页面,防止上述错误。
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
<%@ 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>
页面较为简陋,可以实现定时跳转和立即跳转,并显示错误原因,重新定向至登录页面。
然后,注册页面也开始制作。
文章来自:http://www.cnblogs.com/xll1025/p/6361717.html