实现NETCTOSS登录功能
- 创建用户表:
DROP TABLE IF EXISTS `admin_info`;
create table admin_info(
admin_id int not null auto_increment,
admin_code varchar(30) not null,
password varchar(30) not null,
name varchar(30) not null,
telephone varchar(15),
email varchar(50),
enrolldate datetime default now() not null,
primary key (admin_id)
);
insert into
admin_info
values
(null,'caocao','123','CaoCao','123456789','caocao@tarena.com.cn',default);
- 打开登录页面:
-
创建web.LoginController:
package web;
@Controller
@RequestMapping("/login")
public class LoginController {
@Resource
private LoginService loginService;
@RequestMapping("/toLogin.do")
public String toLogin() {
return "main/login";
}
}
-
创建登录页面:
- 导入项目的图片和样式文件
- 创建登录页面:WEB-INF/jsp/main/login.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>登录</title>
<link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
<link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
</head>
<body class="index">
<div class="login_box">
<table>
<tr>
<td class="login_info">账号:</td>
<td colspan="2"><input name="adminCode" value="" type="text" class="width150" /></td>
<td class="login_error_info"><span class="required">30长度的字母、数字和下划线</span></td>
</tr>
<tr>
<td class="login_info">密码:</td>
<td colspan="2"><input name="password" value="" type="password" class="width150" /></td>
<td><span class="required">30长度的字母、数字和下划线</span></td>
</tr>
<tr>
<td class="login_info">验证码:</td>
<td class="width70"><input name="" type="text" class="width70" /></td>
<td><img src="../images/valicode.jpg" alt="验证码" title="点击更换" /></td>
<td><span class="required"></span></td>
</tr>
<tr>
<td></td>
<td class="login_button" colspan="2">
<a href="#"><img src="../images/login_btn.png" /></a>
</td>
<td><span class="required"></span></td>
</tr>
</table>
</div>
</body>
</html>
- 测试打开登录页面:

- 实现登录验证:
- 创建实体类Admin
package entity;
public class Admin implements Serializable {
private Integer adminId;
private String adminCode;
private String password;
private String name;
private String telephone;
private String email;
private Timestamp enrolldate;
}
- Generate Getters and Setters
- Generate hashCode() and equals()
- Generate toString()
- (添加Mapper接口)数据访问接口:AdminDao:
package dao;
import entity.Admin;
public interface AdminDao {
Admin findByCode(String adminCode);
}
- 在Mapper(映射文件)配置添加SQL, AdminMapper.xml:
<mapper namespace="dao.AdminDao">
<select id="findByCode" parameterType="string" resultType="entity.Admin">
select
admin_id as adminId,
admin_code as adminCode,
password as password,
name as name,
telephone as telephone,
email as email,
enrolldate as enrolldate
from
admin_info
where
admin_code = #{adminCode}
</select>
</mapper>
- 定义业务接口(声明软件业务功能方法 )LoginService:定义登陆功能方法:
- 校验管理员账号和密码:
public interface LoginService {
/**
* 校验管理员账号和密码
* @param adminCode 账号
* @param password 密码
* @return 验证通过时返回管理员对象
* @throws AdminCodeException
* @throws PasswordException
*/
Admin login(String adminCode, String password) throws AdminCodeException, PasswordException;
}
- 创建异常类AdminCodeException,PasswordException:
- public class AdminCodeException extends RuntimeException {}
- public class PasswordException extends RuntimeException {}
- 创建登录业务组件LoginServiceImpl:
@Service("LoginService")
public class LoginServiceImpl implements LoginService {
@Resource
private AdminDao adminDao;
public Admin login(String adminCode, String password) throws AdminCodeException, PasswordException {
Admin admin = adminDao.findByCode(adminCode);
if(admin==null) {
throw new AdminCodeException("账号错误");
} else if(!admin.getPassword().equals(password)) {
throw new PasswordException("密码错误");
} else {
return admin;
}
}
}
- 在LoginController中增加登录验证的方法:
@RequestMapping("/toIndex.do")
public String toIndex() {
return "main/index";
}
@RequestMapping("/checkLogin.do")
public String checkLogin(String adminCode, String password, ModelMap model, HttpSession session){
try {
Admin admin = loginService.login(adminCode, password);
session.setAttribute("admin", admin);
} catch (AdminCodeException e) {
model.addAttribute("message", e.getMessage());
model.addAttribute("adminCode", adminCode);
model.addAttribute("password", password);
return "main/login";
} catch (PasswordException e) {
model.addAttribute("message", e.getMessage());
model.addAttribute("adminCode", adminCode);
model.addAttribute("password", password);
return "main/login";
}
return "redirect:toIndex.do";
}
- 修改登录页面:

- 测试打开登录页面(验证账号/密码功能)


- 创建netctoss主页:WEB-INF/jsp/main/index.jsp
<%@page pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>主页</title>
<link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
<link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
</head>
<body class="index">
<!--导航区域开始-->
<div id="index_navi">
<ul id="menu">
<li><a href="index.html" class="index_on"></a></li>
<li><a href="/NETCTOSS/" class="role_off"></a></li>
<li><a href="admin/admin_list.html" class="admin_off"></a></li>
<li><a href="fee/fee_list.html" class="fee_off"></a></li>
<li><a href="account/account_list.html" class="account_off"></a></li>
<li><a href="service/service_list.html" class="service_off"></a></li>
<li><a href="bill/bill_list.html" class="bill_off"></a></li>
<li><a href="report/report_list.html" class="report_off"></a></li>
<li><a href="user/user_info.html" class="information_off"></a></li>
<li><a href="user/user_modi_pwd.html" class="password_off"></a></li>
</ul>
</div>
</body>
</html>
- 测试打开登录页面(账号密码正确):重定向到首页

DROP TABLE IF EXISTS `admin_info`; create table admin_info( admin_id int not null auto_increment, admin_code varchar(30) not null, password varchar(30) not null, name varchar(30) not null, telephone varchar(15), email varchar(50), enrolldate datetime default now() not null, primary key (admin_id) ); insert into admin_info values (null,'caocao','123','CaoCao','123456789','caocao@tarena.com.cn',default);
-
创建web.LoginController:
package web; @Controller @RequestMapping("/login") public class LoginController { @Resource private LoginService loginService; @RequestMapping("/toLogin.do") public String toLogin() { return "main/login"; } }
-
创建登录页面:
- 导入项目的图片和样式文件
- 创建登录页面:WEB-INF/jsp/main/login.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>登录</title> <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" /> </head> <body class="index"> <div class="login_box"> <table> <tr> <td class="login_info">账号:</td> <td colspan="2"><input name="adminCode" value="" type="text" class="width150" /></td> <td class="login_error_info"><span class="required">30长度的字母、数字和下划线</span></td> </tr> <tr> <td class="login_info">密码:</td> <td colspan="2"><input name="password" value="" type="password" class="width150" /></td> <td><span class="required">30长度的字母、数字和下划线</span></td> </tr> <tr> <td class="login_info">验证码:</td> <td class="width70"><input name="" type="text" class="width70" /></td> <td><img src="../images/valicode.jpg" alt="验证码" title="点击更换" /></td> <td><span class="required"></span></td> </tr> <tr> <td></td> <td class="login_button" colspan="2"> <a href="#"><img src="../images/login_btn.png" /></a> </td> <td><span class="required"></span></td> </tr> </table> </div> </body> </html>
- 测试打开登录页面:
- 创建登录页面:WEB-INF/jsp/main/login.jsp
- 创建实体类Admin
package entity; public class Admin implements Serializable { private Integer adminId; private String adminCode; private String password; private String name; private String telephone; private String email; private Timestamp enrolldate; }- Generate Getters and Setters
- Generate hashCode() and equals()
- Generate toString()
- (添加Mapper接口)数据访问接口:AdminDao:
package dao; import entity.Admin; public interface AdminDao { Admin findByCode(String adminCode); }
- 在Mapper(映射文件)配置添加SQL, AdminMapper.xml:
<mapper namespace="dao.AdminDao"> <select id="findByCode" parameterType="string" resultType="entity.Admin"> select admin_id as adminId, admin_code as adminCode, password as password, name as name, telephone as telephone, email as email, enrolldate as enrolldate from admin_info where admin_code = #{adminCode} </select> </mapper>
- 定义业务接口(声明软件业务功能方法 )LoginService:定义登陆功能方法:
- 校验管理员账号和密码:
public interface LoginService { /** * 校验管理员账号和密码 * @param adminCode 账号 * @param password 密码 * @return 验证通过时返回管理员对象 * @throws AdminCodeException * @throws PasswordException */ Admin login(String adminCode, String password) throws AdminCodeException, PasswordException; }
- 创建异常类AdminCodeException,PasswordException:
- public class AdminCodeException extends RuntimeException {}
- public class PasswordException extends RuntimeException {}
- 创建登录业务组件LoginServiceImpl:
@Service("LoginService") public class LoginServiceImpl implements LoginService { @Resource private AdminDao adminDao; public Admin login(String adminCode, String password) throws AdminCodeException, PasswordException { Admin admin = adminDao.findByCode(adminCode); if(admin==null) { throw new AdminCodeException("账号错误"); } else if(!admin.getPassword().equals(password)) { throw new PasswordException("密码错误"); } else { return admin; } } }
- 在LoginController中增加登录验证的方法:
@RequestMapping("/toIndex.do") public String toIndex() { return "main/index"; } @RequestMapping("/checkLogin.do") public String checkLogin(String adminCode, String password, ModelMap model, HttpSession session){ try { Admin admin = loginService.login(adminCode, password); session.setAttribute("admin", admin); } catch (AdminCodeException e) { model.addAttribute("message", e.getMessage()); model.addAttribute("adminCode", adminCode); model.addAttribute("password", password); return "main/login"; } catch (PasswordException e) { model.addAttribute("message", e.getMessage()); model.addAttribute("adminCode", adminCode); model.addAttribute("password", password); return "main/login"; } return "redirect:toIndex.do"; }
- 修改登录页面:
- 测试打开登录页面(验证账号/密码功能)
- 创建netctoss主页:WEB-INF/jsp/main/index.jsp
<%@page pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>主页</title> <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" /> </head> <body class="index"> <!--导航区域开始--> <div id="index_navi"> <ul id="menu"> <li><a href="index.html" class="index_on"></a></li> <li><a href="/NETCTOSS/" class="role_off"></a></li> <li><a href="admin/admin_list.html" class="admin_off"></a></li> <li><a href="fee/fee_list.html" class="fee_off"></a></li> <li><a href="account/account_list.html" class="account_off"></a></li> <li><a href="service/service_list.html" class="service_off"></a></li> <li><a href="bill/bill_list.html" class="bill_off"></a></li> <li><a href="report/report_list.html" class="report_off"></a></li> <li><a href="user/user_info.html" class="information_off"></a></li> <li><a href="user/user_modi_pwd.html" class="password_off"></a></li> </ul> </div> </body> </html>
- 测试打开登录页面(账号密码正确):重定向到首页
本文介绍了一个名为NETCTOSS系统的登录功能实现过程,包括创建用户表、登录控制器、登录页面、实体类、数据访问接口及映射文件等步骤,并详细描述了登录验证流程。

887

被折叠的 条评论
为什么被折叠?



