自定义MVC登录注册以及树形展示

一:登录注册

1.导入jar包以及实体类等MVC框架

2、建立用户实体类(user)

package com.xly.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;
	}
	
	public User() {
		// TODO Auto-generated constructor stub
	}
	public User(long id, String name, String pwd, int type) {
		super();
		this.id = id;
		this.name = name;
		this.pwd = pwd;
		this.type = type;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
	}
	

}

 2.1、写用户的注册和登录dao方法

package comxly.dao;

import com.xly.entity.User;
import com.zking.util.BaseDao;

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"});
	}
}

 2.2、后台action方法

package com.xly.web;

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

import com.xly.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;

import comxly.dao.UserDao;

public class UserAction extends ActionSupport implements ModelDriver<User>{
	private User user=new User();
	private UserDao userDao=new UserDao();
	@Override
	public User getModel() {
		return user;
	}
public String login(HttpServletRequest req, HttpServletResponse resp) {
	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";
}
}

2.3、mvc.xml文件修改其跳转路径

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

2.4、加入js代码

$(function(){
	$("#bookMenus").tree({
		url:$("#ctx").val()+"/permission.action?methodName=tree"
	});
})

展示结果

用户登录: 

注册界面:

 二:树形结构展示

1、PermissionDao

package com.xly.dao;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.xly.entity.Permission;
 
import com.zking.util.BaseDao;
import com.zking.util.BuildTree;
import com.zking.util.PageBean;
import com.zking.util.TreeVo;
 
public class PermissionDao extends BaseDao<Permission> {
	/*
	 * 变成easyui的tree控件所识别的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<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) {
			System.out.println(p);
			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");
	}
	
	
	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) {
			System.out.println(p);
			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");
	}
	
	
	
	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);
	}
	
	/*	目前:
	 * 是查询所有的菜单数据形成属性的层级结构
	 * 1.select * from t_easyui_Permission where 1=1 and id in(商家菜单id/买家菜单id)
	 * 2.买家/商家的菜单id是在角色权限表t_easyui_role_permission中获取,通过user表中的TYPE字段进行查询
	 * 
	 * */
	
//	public static void main(String[] args) {
//		PermissionDao p = new PermissionDao();
//		try {
//			List<TreeVo<Permission>> l = p.tree(null, null);
//			for (TreeVo<Permission> t : l) {
//				System.out.println(t);
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
}

2、 PermissionAction

package com.xly.web;

import java.util.List;

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

import com.xly.entity.Permission;
import com.xly.entity.RolePermission;
import com.xly.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.TreeVo;

import comxly.dao.PermissionDao;
import comxly.dao.RolePermissionDao;
import comxly.dao.UserDao;

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();
	@Override
	public Permission getModel() {
		// TODO Auto-generated method stub
		return permission;
	}
		public String tree(HttpServletRequest req, HttpServletResponse resp) {
			try {
				User cuser = (User) req.getSession().getAttribute("cuser");
				if(cuser==null) {
					return "toLogin";
				}
				int type = cuser.getType();
				List<RolePermission> RolePermissions = rolepermissionDao.findRolePermission(type);
				StringBuffer sb=new StringBuffer();
				for(RolePermission rp :RolePermissions) {
					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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				try {
					ResponseUtil.writeJson(resp, 0);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			return null;
		}
	
}

3、RolePermissionDao(分权管理) 

package comxly.dao;

import java.util.List;

import com.xly.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);
	}
}

3.1、RolePermission实体类

package com.xly.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 + "]";
	}
	

}

商家显示页面

 

买家显示页面

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值