自定义mvc01

该博客介绍了如何在自定义MVC框架下实现登录注册功能和权限管理。首先,讲解了登录注册的流程,包括导入依赖、创建login.jsp和register.jsp界面,以及实现实体类、DAO和ACTION。接着,详细阐述了左侧树形菜单的构建,涉及Permission实体类、DAO、ACTION和XML配置。最后,展示了权限登录的实现,包括查询用户权限并构建菜单层级结构的方法。
摘要由CSDN通过智能技术生成

思维导图

 一.登录注册

导入所需要的工具类,jar包以及界面

login.jsp  登录界面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网上书城登录</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/fg.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>
 
</head>
<body class="text-center">
<form class="form-signin" action="${pageContext.request.contextPath}/user.action?methodName=login" method="post">
    <h1 class="h3 mb-3 font-weight-normal">用户登录</h1>
    <label for="name" class="sr-only">账号</label>
    <input type="text" id="name" name="name" class="form-control" placeholder="请输入账号" required autofocus>
    <label for="pwd" class="sr-only">密码</label>
    <input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> Remember me
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" id="login">登录</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017-2020</p>
</form>
 
<script>
    $(function () {
 
        <%--$("#login").click(function () {--%>
            <%--$.ajax({--%>
                <%--url:'${pageContext.request.contextPath}/user.action?methodName=login',--%>
                <%--data:"name="+$("#name").val()+"&pwd="+$("#pwd").val(),--%>
                <%--success:function (data) {--%>
 
                <%--}--%>
            <%--});--%>
        <%--});--%>
    })
</script>
</body>
</html>

register.jsp  注册界面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网上书城注册</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/fg.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>
 
</head>
<body class="text-center">
<form class="form-signin" action="${pageContext.request.contextPath}/user.action?methodName=register" method="post">
    <h1 class="h3 mb-3 font-weight-normal">用户注册</h1>
    <label for="name" class="sr-only">账号</label>
    <input type="text" id="name" name="name" class="form-control" placeholder="请输入账号" required autofocus>
    <label for="pwd" class="sr-only">密码</label>
    <input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> Remember me
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" id="">注册</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017-2020</p>
</form>
</body>
</html>

实体类,dao包,web包

①实体类  user实体类

package com.lsy.entity;
 
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;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
	}
	
}

userdao

package com.lsy.dao;
 
import java.util.List;
 
import com.lsy.entity.RolePermission;
import com.lsy.entity.User;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
 
public class UserDao extends BaseDao<User> {
	public User login(User user) throws Exception {
		String sql="select * from t_easyui_user where name='"+user.getName()+"' and pwd='"+user.getPwd()+"'";
		return super.executeQuery(sql, User.class, null).get(0);
	}
	
	
	
	
	public void add(User user) throws Exception {
		String sql= "insert into t_easyui_user(name,pwd) values(?,?)";
		super.executeUpdate(sql, user, new String[] {"name","pwd"});
	}
	
	
	
//public List<User> list(String sql, Class<User> clz, PageBean pageBean) throws Exception {
//		
//		return super.executeQuery(sql, clz, pageBean);
//	}
}

userAction

package com.lsy.web;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.lsy.dao.UserDao;
import com.lsy.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
 
public class UserAction extends ActionSupport implements ModelDriver<User>{
	private User user=new User();
	private UserDao userDao=new UserDao();
	
	public User getModel() {
		return user;
	}
	
	public String login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		try {
			User u = userDao.login(user);
			if(u==null) {
				return "toLogin";
			}
			req.getSession().setAttribute("cuser", u);
		} catch (Exception e) {
			e.printStackTrace();
			return "toLogin";
		}
		//只要数据库中有这个用户,就跳转到主界面
		return "main";
	}
	
	public String register(HttpServletRequest req, HttpServletResponse resp) {
		try {
			userDao.add(user);
			req.setAttribute("msg", "用户名或密码错误");
		} catch (Exception e) {
			e.printStackTrace();
			return "toRegister";
		}
		//如果注册成功,跳转到登录界面
		return "toLogin";
	}
	
}

最后配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/user" type="com.lsy.web.UserAction">
		<forward name="main" path="/bg/mainTemp.jsp" redirect="false" />
		<forward name="toLogin" path="/login.jsp" redirect="true" />
		<forward name="toRegister" path="/register.jsp" redirect="false" />
	</action> 
	
	
	
</config>

二.左侧树形菜单

①.界面代码  mainTemp.jsp界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>后管主界面</title>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/static/js/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui/themes/icon.css">
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/static/js/easyui/jquery.easyui.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/main.js"></script>
</head>
<body class="easyui-layout">
<<input type="text" name="ctx" id="ctx" value="${pageContext.request.contextPath}">
<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">网上书城</div>
<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
    <ul id="bookMenus"></ul>
</div>
<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">底部版权</div>
<div data-options="region:'center',title:'Center'">
    <div id="bookTabs" class="easyui-tabs" style="width:100%;height:100%;">
        <div title="首页" style="padding:20px;display:none;">
            欢迎来到网上书城
        </div>
    </div>
</div>
</body>
</html>

实体类,dao包,web包

  Permission实体类

 
  1.  
    package com.lsy.entity;
     
    public class Permission {
    	private long id;
    	private String name;
    	private String description;
    	private String url;
    	private long pid;
    	private int ismenu;
    	private long displayno;
    	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 getDescription() {
    		return description;
    	}
    	public void setDescription(String description) {
    		this.description = description;
    	}
    	public String getUrl() {
    		return url;
    	}
    	public void setUrl(String url) {
    		this.url = url;
    	}
    	public long getPid() {
    		return pid;
    	}
    	public void setPid(long pid) {
    		this.pid = pid;
    	}
    	public int getIsmenu() {
    		return ismenu;
    	}
    	public void setIsmenu(int ismenu) {
    		this.ismenu = ismenu;
    	}
    	public long getDisplayno() {
    		return displayno;
    	}
    	public void setDisplayno(long displayno) {
    		this.displayno = displayno;
    	}
    	@Override
    	public String toString() {
    		return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid="
    				+ pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
    	}
    	
    }

    PermissionDao类

    package com.lsy.dao;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
    import com.lsy.entity.Permission;
    import com.lsy.entity.RolePermission;
    import com.zking.util.BaseDao;
    import com.zking.util.BuildTree;
    import com.zking.util.PageBean;
    import com.zking.util.TreeVo;
     
    import sun.print.PSStreamPrintService;
     
    public class PermissionDao extends BaseDao<Permission>{
    	/**
    	 * 变成easyUI的true控件所识别的json格式
    	 * 1.Jackson引入进来
    	 * 2.拿到Permission的list集合,先查询数据库
    	 * 3.List<Permission>转换成List<TreeVo>
    	 * 4.通过工具类BuildTree将凭借数据转换成层级数据
    	 * 
    	 */
    	
    	public List<Permission> list(Permission permission, PageBean pageBean) throws Exception {
    		String sql="select * from t_easyui_Permission where 1=1";
    		return super.executeQuery(sql, Permission.class, null);
    	}
    	
    	
    	public List<Permission> listPlus(String ids) throws Exception {
    		String sql="select * from t_easyui_Permission where id in ("+ids+")";
    		return super.executeQuery(sql, Permission.class, null);
    	}
    	
    	public List<TreeVo<Permission>> tree(Permission permission, PageBean pageBean) throws Exception {
    		List<Permission> listPermission = this.list(permission, pageBean);
    		List<TreeVo<Permission>> listvo=new ArrayList<TreeVo<Permission>>();
    		for (Permission p : listPermission) {
    			TreeVo<Permission> vo=new TreeVo<>();
    			vo.setId(p.getId()+"");
    			vo.setText(p.getName());
    			vo.setParentId(p.getPid()+"");
    			Map<String, Object> map=new HashMap<String, Object>();
    			map.put("self",p);
    			vo.setAttributes(map);
    			listvo.add(vo);
    		}
    		return BuildTree.buildList(listvo, "0");
    //		return super.executeQuery(sql, Permission.class, null);
    	}
    	
    	
    	public List<TreeVo<Permission>> treePlus(String ids) throws Exception {
    		List<Permission> listPermission = this.listPlus(ids);
    		List<TreeVo<Permission>> listvo=new ArrayList<TreeVo<Permission>>();
    		for (Permission p : listPermission) {
    			TreeVo<Permission> vo=new TreeVo<>();
    			vo.setId(p.getId()+"");
    			vo.setText(p.getName());
    			vo.setParentId(p.getPid()+"");
    			Map<String, Object> map=new HashMap<String, Object>();
    			map.put("self",p);
    			vo.setAttributes(map);
    			listvo.add(vo);
    		}
    		return BuildTree.buildList(listvo, "0");
    //		return super.executeQuery(sql, Permission.class, null);
    	}
     
    	
    	/**
    	 * 目前:
    	 * 是查询所有的菜单数据形成属性层级结构
    	 * 1.select * from t_easyui_Permission where 1=1 and id in(商家的菜单id)
    	 * 2.select * from t_easyui_Permission where 1=1 and id in(买家的菜单id)
    	 * 买家/商家的菜单id是在角色权限表中获取t_easyui_role_permission获取, 通过user表中的type字段进行查询
    	 */
    	
    	
    	
    }

    PermissionAction类  控制器子类

    package com.lsy.web;
     
    import java.util.List;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import com.lsy.dao.PermissionDao;
    import com.lsy.dao.RolePermissionDao;
    import com.lsy.dao.UserDao;
    import com.lsy.entity.Permission;
    import com.lsy.entity.RolePermission;
    import com.lsy.entity.User;
    import com.zking.framework.ActionSupport;
    import com.zking.framework.ModelDriver;
    import com.zking.util.ResponseUtil;
    import com.zking.util.TreeVo;
     
    public class PermissionAction extends ActionSupport implements ModelDriver<Permission>{
    	private Permission permission=new Permission();
    	private PermissionDao permissionDao=new PermissionDao();
    	private UserDao userDao=new UserDao();
    	private RolePermissionDao rolePermissionDao=new RolePermissionDao();
    	
    	public Permission getModel() {
    		return permission;
    	}
    	
    	public String tree(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    		try {
    		    List<TreeVo<Permission>> tree = pd.tree(null, null);
    		    ResponseUtil.writeJson(resp, tree);
    		} catch (Exception e) {
    			e.printStackTrace();
    			 try {
    				ResponseUtil.writeJson(resp, "0");
    			} catch (Exception e1) {
    				e1.printStackTrace();
    			}
    		}
    		return null;
    	}
     
     
    }

    配置xml文件

    <action path="/permission" type="com.lsy.web.PermissionAction">
        </action>

     界面展示

     三.权限登录
    /**
         * 目前:
         * 是查询所有的菜单数据形成属性层级结构
         * 1.select * from t_easyui_Permission where 1=1 and id in(商家的菜单id)
         * 2.select * from t_easyui_Permission where 1=1 and id in(买家的菜单id)
         * 买家/商家的菜单id是在角色权限表中获取t_easyui_role_permission获取, 通过user表中的type字段进行查询
         */

    ①权限表实体类
     

    package com.lsy.entity;
     
    public class RolePermission {
    	private long rid;
    	private long pid;
    	public long getRid() {
    		return rid;
    	}
    	public void setRid(long rid) {
    		this.rid = rid;
    	}
    	public long getPid() {
    		return pid;
    	}
    	public void setPid(long pid) {
    		this.pid = pid;
    	}
    	@Override
    	public String toString() {
    		return "RolePermission [rid=" + rid + ", pid=" + pid + "]";
    	}
    	
    }

    ②dao类

    package com.lsy.dao;
     
    import java.util.List;
     
    import com.lsy.entity.RolePermission;
    import com.zking.util.BaseDao;
     
    public class RolePermissionDao extends BaseDao<RolePermission>{
    	/**
    	 * 通过user表中的type字段进行查询,查询出商家/买家对应的菜单ID
    	 * @param type
    	 * @return
    	 * @throws Exception
    	 */
    	public List<RolePermission> findRolePermission(int type) throws Exception {
    		String sql="select * from t_easyui_role_Permission where rid="+type;
    		return super.executeQuery(sql, RolePermission.class, null);
    	}
    }

    PermissionDao中增加listPlus以及treePlus方法

    listPlus方法代码

    public List<Permission> listPlus(String ids) throws Exception {
            String sql="select * from t_easyui_Permission where id in ("+ids+")";
            return super.executeQuery(sql, Permission.class, null);
        }

    treePlus方法代码

    public List<TreeVo<Permission>> treePlus(String ids) throws Exception {
            List<Permission> listPermission = this.listPlus(ids);
            List<TreeVo<Permission>> listvo=new ArrayList<TreeVo<Permission>>();
            for (Permission p : listPermission) {
                TreeVo<Permission> vo=new TreeVo<>();
                vo.setId(p.getId()+"");
                vo.setText(p.getName());
                vo.setParentId(p.getPid()+"");
                Map<String, Object> map=new HashMap<String, Object>();
                map.put("self",p);
                vo.setAttributes(map);
                listvo.add(vo);
            }
            return BuildTree.buildList(listvo, "0");
    //        return super.executeQuery(sql, Permission.class, null);
        }

    之后的PermissionAction类中要修改代码
     

    public String tree(HttpServletRequest req, HttpServletResponse resp) throws Exception {
            try {
                User cuser = (User)req.getSession().getAttribute("cuser");
                if(cuser==null) {
                    return "toLogin";
                }
                int type = cuser.getType();
                List<RolePermission> findRolePermission = rolePermissionDao.findRolePermission(type);
                StringBuffer sb=new StringBuffer();
                for (RolePermission rp : findRolePermission) {
                    sb.append(",").append(rp.getPid());
                }
                 List<TreeVo<Permission>> treePlus = permissionDao.treePlus(sb.substring(1));
                
                
    //            List<TreeVo<Permission>> tree = permissionDao.tree(null, null);
                ResponseUtil.writeJson(resp, treePlus);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    ResponseUtil.writeJson(resp, "0");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
            return null;
        }
    

结果展示

①普通用户登录展示

②买家界面展示

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值