当前位置:网站首页>自定义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登录
边栏推荐
- Solve win10 disk occupation of 100%
- Gap locks
- MYSQL导入sqllite表格的两种方法
- Recursive menu query (recursion: check yourself)
- [wechat applet] detailed explanation of applet host environment
- 邮件的收发的展现逻辑之收件箱发件箱以及回复断链的问题
- pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
- Implementation of recommendation system collaborative filtering in spark
- Record locks
- leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
猜你喜欢
![[Shakespeare: keep the fun of being a man]](/img/71/6476f2d58255c78ac8f58fbfc6a0c9.png)
[Shakespeare: keep the fun of being a man]
![[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code](/img/39/716c62d6ca533a7e84704b2c55d072.png)
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
![[wechat applet] detailed explanation of applet host environment](/img/57/582c07f6e6443f9f139fb1af225ea4.png)
[wechat applet] detailed explanation of applet host environment

Use huggingface to quickly load pre training models and datasets in moment pool cloud

阿唐的小帮手

EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
![[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code](/img/2a/b5214e9fa206f1872293c9b9d7bdb6.png)
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code

解决Win10磁盘占用100%

How does win11's own drawing software display the ruler?
随机推荐
Promise期约
C# 音乐
R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
聊聊如何用 Redis 实现分布式锁?
MySQL 悲观锁
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
C Music
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
Win11动态磁贴没了?Win11中恢复动态磁贴的方法
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
记得那两句话
【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
百奥赛图与LiberoThera共同开发全人GPCR抗体药物取得里程碑式进展
食品安全丨无处不在的冷冻食品,你真的了解吗?
Mysql读写锁
MySQL read / write lock
Solve win10 disk occupation of 100%
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
Record locks