当前位置:网站首页>自定义mvc项目登录注册和树形菜单
自定义mvc项目登录注册和树形菜单
2022-07-25 16:23:00 【迟早嘚秃】
Easyui and mvc 项目1_登陆注册权限树形展示
一、登录and注册
导入tomcat和项目所需要的jar包和之前写好的工具类
建立数据库中的User尸体类(用户)
package com.mjx.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 + "]";
}
}
写登陆and注册的dao方法
package com.mjx.dao;
import java.util.List;
import com.mjx.entity.RolePermission;
import com.mjx.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" });
}
}
写UserAction
package com.mjx.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mjx.dao.UserDao;
import com.mjx.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();
@Override
public User getModel() {
// TODO Auto-generated method stub
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";
}
}
修改xml文件,对应跳转的页面
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/user" type="com.dzl.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>
<action path="/Permission" type="com.dzl.web.PermissionAction">
<forward name="toLogin" path="/login.jsp" redirect="true" /> <!--重定向 -->
</action>
</config>
及main.js
$(function(){
$("#bookMenus").tree({
url:$("#ctx").val()+"/Permission.action?methodName=tree"
})
})
运行结果:

二、树形菜单的展示
PermissionAction
package com.mjx.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mjx.dao.PermissionDao;
import com.mjx.dao.RolePermissionDao;
import com.mjx.dao.UserDao;
import com.mjx.entity.Permission;
import com.mjx.entity.RolePermission;
import com.mjx.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.BuildTree;
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() {
// 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) {
e.printStackTrace();
try {
ResponseUtil.write(resp, "0");
} catch (Exception e1) {
e1.printStackTrace();
}
}
return null;
}
}
PermissionDao
package com.mjx.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mjx.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();
// }
// }
}
RolePermissionDao
package com.mjx.dao;
import java.util.List;
import com.mjx.entity.RolePermission;
import com.zking.util.BaseDao;
public class RolePermissionDao extends BaseDao<RolePermission> {
// 通过rid查出对应的pid
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);
}
}
运行结果
卖家javaxl登录
普通买家dzl登录
边栏推荐
猜你喜欢

进程之间的通信(管道详解)

从业务需求出发,开启IDC高效运维之路

The second revolution of reporting tools
![[Shakespeare: keep the fun of being a man]](/img/71/6476f2d58255c78ac8f58fbfc6a0c9.png)
[Shakespeare: keep the fun of being a man]

2w字详解数据湖:概念、特征、架构与案例

Breakthrough in core technology of the large humanoid Service Robot Walker x

tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框

Implementation of recommendation system collaborative filtering in spark
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]

Exploration of 6-wire SPI transmission mode
随机推荐
R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
Intention lock
权限管理-删除菜单(递归)
Pagehelper.startpage is not effective
测试驱动开发(TDD)在线练功房 | 9月17日开课
泰雷兹推出解决方案,助力SAP客户控制云端数据
C # simulation lottery
Win11自带画图软件怎么显示标尺?
mysql 表读锁
slf4j 搭配 log4j2 处理日志
C# 模拟抽奖
墨天轮高分技术文档分享——数据库安全篇(共48个)
聊聊如何用 Redis 实现分布式锁?
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
在 NgModule 里通过依赖注入的方式注册服务实例
01. A simpler way to deliver a large number of props
LVGL 7.11 tileview界面循环切换
[zeloengine] summary of pit filling of reflection system
leetcode:154. 寻找旋转排序数组中的最小值 II【关于旋转排序数组的中后定位二分法】
Google Earth Engine——全球建筑物GlobalMLBuildingFootprints矢量集合下载