一、思维导图
二、项目部署
1、在tomcat中的webapps文件中加入mvc.w
?
2、在tomcat的bin目录下运行startup.bat
3、在webapps文件中的MVC中复制文件
?
三、权限登录
商家与买家所登录看到的界面不一致
1、登录方法/注册
①、用户实体类
package com.dhm.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.dhm.dao;
import java.util.List;
import com.dhm.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"});
}
// public List<User> list(String sql, Class<User> clz, PageBean pageBean) throws Exception {
//
//
// return super.executeQuery(sql, clz, pageBean);
// }
}
③、UserAction
package com.dhm.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dhm.dao.UserDao;
import com.dhm.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;
}
/**
* 用户登录方法调用
* @param req
* @param resp
* @return
*/
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";
}
}
④、mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/user" type="com.dhm.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.dhm.web.PermissionAction">
</action>
</config>
效果展示:
登录界面
注册界面
?
2、树形菜单
①、Permission实体类
package com.dhm.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.dhm.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dhm.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, pageBean);
}
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");
}
/**
* 目前:
* 是查询所有的菜单数据形成的树形层级结构
* 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)
* 3、买家/商家的菜单id在角色权限表t_easyui_role_permission中获取,通过user表中的type属性
*/
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");
}
}
③、PermissionAction
package com.dhm.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dhm.dao.RolePermissionDao;
import com.dhm.dao.permissionDao;
import com.dhm.entity.Permission;
import com.dhm.entity.RolePermission;
import com.dhm.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 RolePermissionDao Rolepermissiondao=new RolePermissionDao();
public Permission getModel() {
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> 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;
}
}
④、RolePermission
package com.dhm.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 + "]";
}
}
⑤、RolePermissionDao
package com.dhm.dao;
import java.util.List;
import com.dhm.entity.RolePermission;
import com.zking.util.BaseDao;
public class RolePermissionDao extends BaseDao<RolePermission>{
/**
* 目前:
* 是查询所有的菜单数据形成的树形层级结构
* 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)
* 3、买家/商家的菜单id在角色权限表t_easyui_role_permission中获取,通过user表中的type属性
*/
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);
}
}
效果展示
买家树形菜单
商家树形菜单
?
----------------没有了-----------------
?
|