前言:給大家講解EasyUi專案《網上書城》
*權限:不同用戶登錄時,樹形選單會展示出不同的效果
碼字不易,點個關注
轉載請說明!
開發工具:eclipse,MySQL
思維導圖:

目錄
1、目標
2、思路,代碼以及效果展示
1、登錄、注冊
2、權限
2.1、user表中type中1為商家,2為買家
可以根據用戶的type值來登錄不同的界面
2.2、實作權限登錄的思路
2.3、代碼
1、目標
1、登錄、注冊
2、權限樹形展示(不同用戶登錄時,樹形選單會展示出不同的效果)
2、思路,代碼以及效果展示
1、登錄、注冊
登錄、注冊個人覺得比較簡單,思路如下:
1、在物體類entity創建user類
2、在dao層中寫好相應的方法(登錄login、注冊register方法)
3、然后在子控制器中寫好對應的方法,
4、最后到組態檔中xml寫好相應路徑
user物體類
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 + "]";
}
}
dao層
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 class UserAction extends ActionSupport implements ModelDriver<User> {
private User user = new User();
private UserDao userDao = new UserDao();
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("mag", "用戶名密碼錯誤");
} catch (Exception e) {
e.printStackTrace();
return "toRegister";
}
//如果注冊成功,跳轉到登錄界面
return "toLogin";
}
}
組態檔hpw.xml寫好相應路徑
<action type="com.hpw.web.UserAction" path="/user">
<forward path="/bg/mainTemp.jsp" redirect="false" name="main"/>
<forward path="/login.jsp" redirect="true" name="toLogin"/>
<forward path="/register.jsp" redirect="false" name="toRegister"/>
</action>
2、權限
2.1、user表中type中1為商家,2為買家
可以根據用戶的type值來登錄不同的界面

2.2、實作權限登錄的思路
1、將兩個表關聯起來,其中Permission中的id與pid行成主外鍵關系,RolePermission中的rid與 pid行成父子關系,
2、從兩個表中得到type對應的選單號將其顯示
2.3、代碼
PermissionDao:
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 {
//ids="1,2,3,4,5,6,7,8,9,14";
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> list = this.list(permission, pageBean);
List<TreeVo<Permission>> listVo = new ArrayList<TreeVo<Permission>>();
for (Permission p : list) {
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>> treePuls(String ids) throws Exception {
List<Permission> list = this.listPlus(ids);
List<TreeVo<Permission>> listVo = new ArrayList<TreeVo<Permission>>();
for (Permission p : list) {
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:
public class PermissionAction extends ActionSupport implements ModelDriver<Permission> {
Permission permission = new Permission();
PermissionDao permissionDao = new PermissionDao();
UserDao userDao = new UserDao();
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> findRolePermissions = rolePermissionDao.findRolePermission(type);
StringBuffer sb = new StringBuffer();
for (RolePermission rp : findRolePermissions) {
sb.append(",").append(rp.getPid());
}
//ids="1,2,3,4,5,6,7,8,9,14";
//List<Permission> listPlus = permissionDao.listPlus(sb.substring(1));
List<TreeVo<Permission>> treePuls = permissionDao.treePuls(sb.substring(1));
//List<TreeVo<Permission>> tree = permissionDao.tree(null, null);
ResponseUtil.writeJson(resp, treePuls);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, "0");
} catch (IOException e1) {
e1.printStackTrace();
}
}
return null;
}
}
RolePermissionDao:
package com.zking.dao;
import java.util.List;
import com.hpw.entity.RolePermission;
import com.hpw.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);
}
}
效果展示:
買家身份登陸

商家身份登陸

到這里就結束了,其中重點是權限登錄,當中的邏輯比較復雜
歡迎大佬指點
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/303025.html
標籤:其他
上一篇:基于Opencv的手勢識別
下一篇:ABAP開發知識點整理
