网上书城(功能模块一)

目录

  • 前言
    • 目标
      • 表结构(设计)
      • 步骤
      • entity实体类
      • dao层
      • web层
      • 页面

前言

接上一篇文章通过结合bootstrap官网模型示例+Hbuilder工具绘制好的前台原型界面 跳转到原型界面

目标

1.使用自定义mvc编写模式
2.通过编码实现界面与数据库进行数据绑定
3.用户注册,用户登录,用户权限


表结构(设计)

1.用户注册表
网上书城(功能模块一)_第1张图片

步骤

entity实体类

1.注册类列字段对应数据库

package com.houzhihong.entity;
/**
 * 
 * @author houzhihong
 *
 * 2020年7月2日
 */
public class User {
	    private long id;
	    private String name;
	    private String pwd;
	    private int type;
		public long getId() {
			return id;
		}
		public void setId(long id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPwd() {
			return pwd;
		}
		public void setPwd(String pwd) {
			this.pwd = pwd;
		}
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public User(long id, String name, String pwd, int type) {
			super();
			this.id = id;
			this.name = name;
			this.pwd = pwd;
			this.type = type;
		}
		public User() {
			super();
		}
		@Override
		public String toString() {
			return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
		}
	    
}


dao层

1.用户注册新增,登录查询方法。

package com.houzhihong.dao;

import java.sql.SQLException;
import java.util.List;

import com.houzhihong.entity.User;
import com.houzhihong.util.BaseDao;
import com.houzhihong.util.StringUtils;

public class UserDao extends BaseDao<User> {
	/**
	 * 
	 * @param user
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public User login(User user) throws InstantiationException, IllegalAccessException, SQLException {
		String name = user.getName();
		String pwd = user.getPwd();
		String sql = "select * from t_easyui_user where true";
		if (StringUtils.isNotBlank(name)) {
			sql += " and name = '" + name + "' ";
		}
		if (StringUtils.isNotBlank(pwd)) {
			sql += " and pwd = '" + pwd + "'";
		}
		List<User> list = super.executeQuery(sql, User.class, null);

		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 
	 * @param user
	 * @return
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int add(User user) throws NoSuchFieldException, SecurityException, IllegalArgumentException,
			IllegalAccessException, SQLException {
		String sql = "insert into t_easyui_user(name,pwd) values(?,?) ";
		return super.executeUpdate(sql, user, new String[] { "name", "pwd" });
	}
}


2.RoleDao用户权限管理

package com.houzhihong.dao;

import com.houzhihong.entity.Permission;
import com.houzhihong.entity.RolePermission;
import com.houzhihong.util.BaseDao;

import java.util.List;
public class RoleDao extends BaseDao<RoleDao> {
    //    获取角色的权限id
    public List<Role> getPids(long rid) throws Exception {
        String sql = "select * from t_easyui_role_permission where rid = " + rid;
        return super.executeQuery(sql, null, RolePermission.class);
    }
}


web层

1.处理用户注册,登录的业务逻辑action

package com.houzhihong.web;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houzhihong.dao.UserDao;
import com.houzhihong.entity.User;
import com.houzhihong.mvc.framework.ActionSupport;
import com.houzhihong.mvc.framework.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User> {
	private User user = new User();
	private UserDao userDao = new UserDao();

	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}

	/**
	 * 登录
	 * @param req
	 * @param resp
	 * @return
	 */
	public String login(HttpServletRequest req, HttpServletResponse resp) {
		try {
			User current = this.userDao.login(user);
			if (current == null) {
				return "success";
			}
			req.getSession().setAttribute("currentUser", current);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
			return "login";
		}
		return "success";
	}

	/**
	 * 注册
	 * @param req
	 * @param resp
	 * @return
	 */
	public String add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			this.userDao.add(user);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "register";
		}
		return "login";

	}
}



2.树形菜单action

package com.houzhihong.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.houzhihong.dao.PermissionDao;
import com.houzhihong.dao.RoleDao;
import com.houzhihong.entity.Permission;
import com.houzhihong.entity.RolePermission;
import com.houzhihong.entity.User;
import com.houzhihong.framework.ActionSupport;
import com.houzhihong.framework.ModelDriven;
import com.houzhihong.utils.ResponseUtil;
import com.houzhihong.vo.TreeVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class PermissionAction extends ActionSupport implements ModelDriven<Permission> {
    private Permission permission = new Permission();
    private PermissionDao permissionDao = new PermissionDao();
    private RoleDao roleDao = new RoleDao();

    public String menuTree(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper om = new ObjectMapper();
        try {
            User currentUser = (User) request.getSession().getAttribute("currentUser");
            List<RolePermission> pids = rolePermissionDao.getPids(currentUser.getType());
            StringBuilder sb = new StringBuilder();
            for (RolePermission pid : pids) {
                sb.append(",").append(pid.getPid());
            }

            List<TreeVo<Permission>> treeVos = this.permissionDao.menuTreeHandler(sb.substring(1));
            String json = om.writeValueAsString(treeVos);
            ResponseUtil.write(response,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Permission getModel() {
        return permission;
    }
}


页面

1.用户注册页面及登录
网上书城(功能模块一)_第2张图片

<%@ 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">
	<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js">

		
		
<title>注册界面</title>
</head>
<body>
		<style>
			.bxs-row {
            margin-bottom:12px;
        }
        .logo-box {
            width:404px;
            margin:120px auto;
            border:1px solid #e5e5e5;
            border-radius:4px;
            box-shadow:0 4px 18px rgba(0,0,0,0.2);
            position:relative;
            overflow:hidden;
            height:360px;
        }
        .login {
            position:absolute;
            width:320px;left:0;
            top:0;
            padding: 42px 42px 36px;
            transition:all 0.8s;
        }
        .username,.password,.btn {
            height: 44px;
            width: 100%;
            padding: 0 10px;
            border: 1px solid #9da3a6;
            background: #fff;
            text-overflow: ellipsis;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            -khtml-border-radius: 4px;
            border-radius: 4px;
            -webkit-box-shadow: none;
            -moz-box-shadow: none;
            box-shadow: none;
            color: #000;
            font-size: 1em;
            font-family: Helvetica,Arial,sans-serif;
            font-weight: 400;
            direction: ltr;
            font-size:13px;
        }
        .submit {
            background-color: #0070ba;
            color:#fff;
            border:1px solid #0070ba;
        }
        .submit:hover {
            background-color:#005ea6;
        }
        .verBox {
            position:absolute;
            width:100%;
            text-align:center;
            left:404px;
            top:0;
            opacity:0;
            transition:all 0.8s;
            padding-top:55px;
        }
        .err {
            margin:12px 0 0;
            line-height:12px;
            height:12px;
            font-size:12px;
            color:red;
        }
</style>

	</head>
	
	<body>
		<form action="${pageContext.request.contextPath}/user.action?methodName=login" method="post">
		<div style="text-align:center;">
			用户注册
		</div>
		
		<div class="logo-box">
			<div class="login" style="">
			
				<div class="bxs-row">
					<input type="text" class="username" name="name" placeholder="请输入用户名" >
					<p class=" err err-username"></p>
				</div>
				<div class="bxs-row">
					<input type="password" class="password" name="pwd" placeholder="请输入密码">
					<p class="err err-password"></p>
				</div>
				<div class="bxs-row">
					<input type="submit" class="submit btn" value="注册">
				</div>
			</div>
			
		</div>
		</form>

</body>
</html>

2.用户登录后台使用的权限
网上书城(功能模块一)_第3张图片

3.管理员登录后台使用的权限
网上书城(功能模块一)_第4张图片

你可能感兴趣的:(网上书城(功能模块一))