当前位置:网站首页>自定义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登录
边栏推荐
猜你喜欢

Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强

优必选大型仿人服务机器人Walker X的核心技术突破

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

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
![[Shakespeare: keep the fun of being a man]](/img/71/6476f2d58255c78ac8f58fbfc6a0c9.png)
[Shakespeare: keep the fun of being a man]

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

Differences between cookies, cookies and sessions

Talk about how to use redis to realize distributed locks?

使用Huggingface在矩池云快速加载预训练模型和数据集
![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]
随机推荐
IaaS基础架构云 —— 云网络
Simple rotation map and hamster beating
什么是链游系统开发?链游系统开发如何制作
吴恩达逻辑回归2
递归菜单查询(递归:自己查自己)
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
聊聊如何用 Redis 实现分布式锁?
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
linux内核源码分析之页表缓存
Solve win10 disk occupation of 100%
Recommended collection, which is probably the most comprehensive coding method summary of category type features
mysql 表写锁
pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
国债年化利率太低了,有比国债逆回购年化利率还要高的理财产品吗?
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
What is a physical firewall? What's the effect?
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
Talk about how to use redis to realize distributed locks?
Communication between processes (pipeline details)