SSM框架搭建见我上篇博客:
https://blog.csdn.net/x_san3/article/details/81537461
本篇为实现简单的登录注册功能,需要添加的代码和注意点如下:
1. Dao层添加这两个接口
// 添加用户
public void addUser(User user);
// 根据用户名查询用户
// 注解的两个参数会自动封装成map集合,括号内即为键
public User findByUsername(@Param("name") String name);
2. UserMapper.xml (映射文件)添加语句
<insert id="addUser" parameterType="User">
insert into t_user(USER_NAME, USER_PASSWORD) values(#{userName}, #{userPassword})
</insert>
<!--注意这里的参数类型是parameterType而不是parameterMap,因为返回的是单个类型 -->
<select id="findByUsername" parameterType="Map" resultType="User">
select t.USER_NAME,t.USER_PASSWORD from t_user t where USER_NAME=#{name}
</select>
3. UserService.java 添加
// 用户注册
void regist(User user);
// 用户登录
User login(String name, String password);
4. UserServiceImpl.java 添加
public void regist(User user) {
userDao.addUser(user);
}
public User login(String name, String password) {
User user = userDao.findByUsername(name);
if(user != null && user.getUserPassword().equals(password)){
return user;
}
return null;
}
5. 控制层UserController.java 添加
@RequestMapping("/regist")
public String regist(){
return "regist";
}
@RequestMapping("/doRegist")
public String doRegist(User user, Model model){
System.out.println(user.getUserName());
userService.regist(user);
return "success";
}
@RequestMapping("/login")
public String checkLogin(User user, Model model){
user = userService.login(user.getUserName(), user.getUserPassword());
//若有user则添加到model里并且跳转到成功页面
if(user != null){
model.addAttribute("user",user);
return "success";
}
System.out.println(user);
return "fail";
}
@RequestMapping("/index")
public String index(){
return "index";
}
6. WEB-INF/view/文件夹下建jsp页面
regist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>用户注册页面</title>
</head>
<body>
<h3>注册页面</h3>
<hr/>
<form action="${pageContext.request.contextPath}/user/doRegist" method="post">
<table>
<tr height="35px">
<td width="100px">用户名</td>
<td width="270px">
<input type="text" name="userName">
</td>
</tr>
<tr height="35px">
<td>密码</td>
<td>
<input type="text" name="userPassword">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="regist" value="注册"/>
</td>
</tr>
</table>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<strong> welcome,${sessionScope.user.username}! </strong>
</div>
this is success page!
<%-- <a href="${pageContext.request.contextPath}/user/anotherpage">点我跳到另一个页面</a> --%>
<form action="${pageContext.request.contextPath}/user/index">
<table>
<tr>
<td><input type="submit" value="退出登录"></td>
</tr>
</table>
</form>
</body>
</html>
index.jsp(用于退出登录到首页)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input name="userName" type="text"></td>
</tr>
<tr>
<td>密码:</td>
<td><input name="userPassword" type="password"></td>
</tr>
<tr >
<td><input type="submit" value="登录" ></td>
<td><input type="button" value="注册" onclick="window.location.href='/user/regist'"> </td>
</tr>
</table>
</form>
</body>
</html>
fail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
this is a fail page!
</body>
</html>
说明:可以直接地址栏输入http://localhost:8080/user/index进到首页进行登录注册