SpringMVC用例篇之用户登录系统详细开发流程

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一.用户登录系统环境配置:

1.1:开发环境:

Windows7(x86_64);Eclipse Java EE IDE for Web Developers.Version: Oxygen.1a Release (4.7.1a); java version "1.8.0_73"

1.2:开发框架:

spring-framework-5.0.3.RELEASE   DButils

1.3:所需jar包截图:

SpringMVC用例篇之用户登录系统详细开发流程_第1张图片

二.数据库的设计:

SpringMVC用例篇之用户登录系统详细开发流程_第2张图片

SpringMVC用例篇之用户登录系统详细开发流程_第3张图片

三.开发流程:

 1.1.配置web.xml文件

复制代码

1 
 2   
 3           DispatchcerServletSpringMVC
 4           org.springframework.web.servlet.DispatcherServlet
 5           
 6           
 7                   contextConfigLocation
 8                   classpath:SpringMVC.xml
 9           
10   
11   
12   
13           DispatchcerServletSpringMVC
14           *.action
15   

复制代码

1.2.配置SpringMVC.xml文件

复制代码

1 
 2 
 9     
10      
11     
12     
13     
14     
15     
16     
17          
18         
19         
20         
21     
22 

复制代码

1.3.C3P0数据库连接池的配置:

复制代码

1 
 2 
 3    
 4       com.mysql.jdbc.Driver
 5       jdbc:mysql://localhost:3306/springmvc
 6       root
 7       root
 8     2
 9     5
10   
11 

复制代码

1.4.视图层目录结构:

SpringMVC用例篇之用户登录系统详细开发流程_第4张图片

1.5.各层关键代码展示:

  <1>.view层:

   @1.login.jsp页面:

复制代码

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 
 4 
 5 
 6 
 7 
 8 
17 Admin Login
18 
19 
20 
25     
26
27
${error_msg }
28

AdminLogin

29
30
31
32 52
53
54
55 56 62 63

复制代码

   @2.register.jsp页面:

复制代码

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 
 4 
 5 
 6 
 7 
 8 
17 Register Admin
18 
19 
20         
21
22
${msg }
23

RegisterAdmin

24
25
26
27 56
57
58
59 60

复制代码

   @3.adminWelcome.jsp页面:

复制代码

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3     <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
  4 
  5 
  6 
  7 
  8 
  9 
 18 
 36 AdminWelcome
 37 
 38 
 39 
 40 
 46             
47 52
53
54 ${username }:欢迎你! 55 56
57
58 63
64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
账号姓名密码邮箱修改销毁相册
${adminInfo.account }${adminInfo.name }${adminInfo.passwd }${adminInfo.email }
86
87
88 93
94
95
96
97 98
99 100
101
102 103
104
105 106
107
108 109
110
111
112
113
114
115
116
117
118 119   

复制代码

  <2>.Handler代码展示:

    @1.AdminOperationHandler

复制代码

1 @Controller
 2 @RequestMapping("/admin")
 3 public class AdminOperationHandler {
 4     
 5     private AdminOperationService adminService;
 6     public  AdminOperationHandler() {
 7         this.adminService = new AdminOperationServiceImpl();
 8     }
 9     /*账户登录校验*/
10     @RequestMapping(path={"adminCheck.action"},method= {RequestMethod.POST})
11     public String adminCheck(Admin admin,HttpSession session,HttpServletRequest request ) {
12         try {
13             int temp = adminService.adminCheck(admin);
14             if( temp != -1) {
15                 session.setAttribute("username", admin.getName());
16                 session.setAttribute("aid", temp);
17                 return "forward:/admin/adminQuery.action";
18             }
19         } catch (SQLException e) {
20             e.printStackTrace();
21         }
22         request.setAttribute("error_msg", "用户名或者密码错误!");
23         return "forward:/login.jsp";
24     }
25     /*账户注册*/
26     @RequestMapping(path= {"adminAdd.action"},method= {RequestMethod.POST})
27     public String adminAdd(Admin admin,HttpServletRequest request) {
28         String msg = "";
29         try {
30             if(adminService.adminAdd(admin)) {
31                 msg = "用户添加成功!";
32             }else {
33                 msg = "用户添加失败!";
34             }
35         } catch (SQLException e) {
36             e.printStackTrace();
37         }
38         request.setAttribute("msg", msg);
39         return "forward:/register.jsp";
40     }
41     /*账户销毁*/
42     @RequestMapping("/adminDelete.action")
43     public String adminDelete(HttpServletRequest request,HttpSession session) {
44         try {
45             if (!adminService.adminDelete((Integer)session.getAttribute("aid"))) {
46                 request.setAttribute("error_msg", "账户销毁失败!");
47                 return "forward:/admin/adminQuery.action";
48             }
49         } catch (SQLException e) {
50             e.printStackTrace();
51         }
52         return "redirect:/login.jsp";
53     }
54     /*账户更新*/
55     @RequestMapping(path="adminAlter.action",method=RequestMethod.POST)
56     public String adminAlter(int id,Admin admin,HttpServletRequest request) {
57         try {
58             if (adminService.adminAlter(id, admin)) {
59                 request.setAttribute("error_msg", "信息更新成功!");
60             }else {
61                 request.setAttribute("error_msg", "信息更新失败!");
62             }
63         } catch (SQLException e) {
64             request.setAttribute("error_msg",e.getMessage());
65             e.printStackTrace();
66         }
67         return "forward:/admin/adminQuery.action";
68     }
69     /*账号查询*/
70     @RequestMapping("/adminQuery.action")
71     public String adminQuery(HttpServletRequest request,HttpSession session) {
72         Admin admin;
73         try {
74             admin = adminService.adminQuery((Integer)session.getAttribute("aid"));
75             request.setAttribute("adminInfo", admin);
76         } catch (SQLException e) {
77             e.printStackTrace();
78         }
79         return "adminWelcome";
80     }
81     /*账户注销*/
82     @RequestMapping("/adminLogout.action")
83     public String adminLogout(HttpServletRequest request,HttpSession session) {
84         session.invalidate();
85         request.setAttribute("off_msg", "您将退出了会话状态!");
86         return "forward:/login.jsp";
87     }
88 }

复制代码

  <3>.Service层代码展示:

     @1.AdminOperationService接口展示:

复制代码

1 public interface AdminOperationService {
 2 
 3     /* 增加管理员 */
 4     public boolean adminAdd(Admin admin) throws SQLException;
 5 
 6     /* 删除管理员 */
 7     public boolean adminDelete(int aid) throws SQLException;
 8 
 9     /* 修改管理员 */
10     public boolean adminAlter(int aid, Admin admin) throws SQLException;
11 
12     /* 检查账号 */
13     public int adminCheck(Admin admin) throws SQLException;
14     
15     /*查询账号信息*/
16     public Admin adminQuery(int id) throws SQLException;
17 }

复制代码

    @2.实现类AdminOperationServiceImpl展示:

复制代码

public class AdminOperationServiceImpl implements AdminOperationService {
    
    private AdminOperationDao adminDao;
    public AdminOperationServiceImpl() {
        this.adminDao = new AdminOperationDaoImpl();
    }
    
    @Override
    public boolean adminAdd(Admin admin) throws SQLException {
        boolean isTrue = false;
        if (0 != adminDao.adminAdd(admin)) {
            isTrue = true;
        }
        return isTrue;
    }

    @Override
    public boolean adminDelete(int aid) throws SQLException {
        boolean isTrue = false;
        if (adminDao.adminDelete(aid) != 0) {
            isTrue = true;
        }
        return isTrue;
    }

    @Override
    public boolean adminAlter(int aid, Admin admin) throws SQLException {
        boolean isTrue = false;
        if(adminDao.adminAlter(aid, admin) != 0) {
            isTrue = true;
        }
        return isTrue;
    }

    @Override
    public int adminCheck(Admin admin) throws SQLException {
        int user_id;
        user_id = adminDao.adminCheck(admin);
        return user_id;
    }

    @Override
    public Admin adminQuery(int id) throws SQLException {
        Admin admin;
        admin = adminDao.adminQuery(id);
        return admin;
    }

}

复制代码

  <4>.Dao层代码展示:

    @1.AdminOperationDao接口展示:

复制代码

public interface AdminOperationDao {
    
    /*增加管理员*/
    public int adminAdd(Admin admin) throws SQLException;
    
    /*删除管理员*/
    public int adminDelete(int aid) throws SQLException;
    
    /*修改管理员*/
    public int adminAlter(int aid,Admin admin) throws SQLException;
    
    /*检查账号*/
    public int adminCheck(Admin admin) throws SQLException;
    
    /*查询账号*/
    public Admin adminQuery(int id) throws SQLException;
}

复制代码

    @实现类AdminOperationDaoImpl展示:

复制代码

1 public class AdminOperationDaoImpl implements AdminOperationDao {
 2     
 3     private QueryRunner qr;
 4     public AdminOperationDaoImpl() {
 5         this.qr = new QueryRunner(MyDataSource.getDataSource());
 6     }
 7     
 8     @Override
 9     public int adminAdd(Admin admin) throws SQLException {
10         String sql = "insert into admin(a_account,a_name,a_passwd,a_email) values(?,?,?,?) ";
11         return qr.update(sql, admin.getAccount(),admin.getName(),admin.getPasswd(),admin.getEmail());
12     }
13 
14     @Override
15     public int adminDelete(int aid) throws SQLException {
16         int flag = 0;
17         String sql = "delete from admin where a_id=?";
18         flag = qr.update(sql, aid);
19         return flag;
20     }
21 
22     @Override
23     public int adminAlter(int aid, Admin admin) throws SQLException {
24         int flag = 0;
25         String sql = "update admin set a_account=?,a_name=?,a_passwd=?,a_email=? where a_id=?";
26         flag = qr.update(sql, admin.getAccount(),admin.getName(),admin.getPasswd(),admin.getEmail(),admin.getId());
27         return flag;
28     }
29 
30     @Override
31     public int adminCheck(Admin admin) throws SQLException {
32         int user_id = -1; 
33         String sql = "select a_id id from admin where a_name=? and a_passwd=?";
34         Admin user = qr.query(sql, new BeanHandler(Admin.class),admin.getName(),admin.getPasswd());
35         if (null != user) {
36             user_id = user.getId();
37         }
38         return user_id;
39     }
40 
41     @Override
42     public Admin adminQuery(int id) throws SQLException {
43         Admin admin;
44         String sql = "select a_id id,a_account account,a_name name,a_passwd passwd,a_email email from admin where a_id=?";
45         admin = qr.query(sql, new BeanHandler(Admin.class), id);
46         return admin;
47     }
48 
49 }

复制代码

  <5>.Utils类展示:

    @数据库连接池提供数据源类MyDataSource展示:

复制代码

1 public class MyDataSource {
 2     private static DataSource ds = new ComboPooledDataSource("hello");
 3     private MyDataSource() {
 4     }
 5     public static DataSource getDataSource() {
 6         return ds;
 7     }
 8     public static Connection getConnection() {
 9         Connection con = null;
10         try {
11             con = ds.getConnection();
12         } catch (SQLException e) {
13             e.printStackTrace();
14         }
15         return con;
16     }
17 }

复制代码

四.用户登录系统展示:

SpringMVC用例篇之用户登录系统详细开发流程_第5张图片

SpringMVC用例篇之用户登录系统详细开发流程_第6张图片

SpringMVC用例篇之用户登录系统详细开发流程_第7张图片

SpringMVC用例篇之用户登录系统详细开发流程_第8张图片

SpringMVC用例篇之用户登录系统详细开发流程_第9张图片

转载于:https://my.oschina.net/architectliuyuanyuan/blog/1621823

你可能感兴趣的:(SpringMVC用例篇之用户登录系统详细开发流程)