目录
总结:思路如下。 在你的实体类中创建一个类,在层中编写相应的方法,登录并注册方法,然后在子控制器中编写相应的方法。
前言:先介绍一下EasyUi项目“网上书店”。
*权限:不同的用户登录后会在树形菜单中看到不同的效果。
码字并不容易。 请点击。
如需转载,请注明解释。
开发工具:eclipse、MySQL
思维导图:
目录
1.目标
2.展示想法、代码和效果
1.登录并注册
2.权限
2.1、users表中,类型1为卖家,类型2为买家
可以根据用户类型值登录不同界面
2.2 . 实现授权登录的思路
2.3,代码
1.目标
1.登录注册
2.查看权限树(不同用户登录后会在树形菜单中看到不同的效果)
2.展示想法、代码、效果
1.登录、注册
登录、注册个人认为思路是:
1.在你的实体类中创建一个用户类
2.(登录、注册方法)
3.接下来在子控制器中编写相应的方法。
4.编写p>
User实体类对应的路径。
公共类 User {private long id;private String name. ;private String pwd;private int type;public long getId() {返回值;}public void setId(long id) {this.id = id; }public String getName() {返回值;}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;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type= " + type + "]";}}
dao 层
public 用户登录(User user) 抛出异常 {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) names(?,?)";super.executeUpdate(sql, user , new String [] {"name","pwd"});}
子控制器层
public class UserAction extends ActionSupportimplements ModelDriver {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
2.权限
2.1.在users表中,类型1是卖家,类型2是买家。 % E6% 8D%AE%E7%94%A8%E6%88%B7%E7%9A%84 类型%E5%80%BC%E6%9D%A5%E7%99%BB%E5%BD%95 % E4% B8% 8D%E5%90%8C%E7%9A%84%E7%95%8C%E9%9D%A2">基于以下标准: ,您可以登录不同的界面。
2.2. 权限登录思路实现
1. 关联两个表。 Permission 的 id 和 pid 行形成主外键关系,RolePermission 的 id 和 pid 行形成父子关系。
2.从两个表中获取类型对应的菜单号并显示。
2.3. 代码
PermissionDao:
public List list(Permission 权限, PageBean pageBean) 抛出异常 {String sql = "select * t_easyui_Permission where 1=1";return super .executeQuery(sql , Permission.class, pageBean);}public List 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> Tree(Permission, PageBean 页面 bean)抛出异常 {List list = this.list(permission, pageBean);List<TreeVo> listVo = new ArrayList<TreeVo>();for (Permission p : list) {TreeVo vo = new TreeVo();vo.setId(p.getId() + "");vo.setText(p.getName());vo.setParentId(p.getPid() + "");Map map = new HashMap();map.put("self", p);vo.setAttributes(map);listVo.add(vo);}return BuildTree.buildList(listVo, " 0");} public List<TreeVo>treePuls(String ids) 抛出异常 {List list = this.listPlus(ids);List<TreeVo> listVo = new ArrayList<TreeVo >();for (权限 p : list) {TreeVo vo = new TreeVo();vo.setId(p.getId() + "");vo.setText(p.getName()); vo.setParentId(p.getPid() + "");Map 地图= new HashMap();map.put("self", p);vo.setAttributes(map);listVo.add(vo);}return BuildTree.buildList(listVo, "0");}
PermissionAction:
公共类 PermissionAction 扩展 ActionSupportimplements ModelDriver {Permission 权限 = new Permission(); PermissionDao PermissionDao = new PermissionDao();UserDao userDao = new UserDao();RolePermissionDao rolePermissionDao = new RolePermissionDao();public Permission getModel() {返回权限;}public String Tree(HttpServletRequest req, HttpServletResponse resp) {try {User cuser = (用户) req.getSession().getAttribute("cuser");if(cuser == null) {return "toLogin";}int type = cuser.getType();List 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 listPlus = PermissionDao.listPlus(sb.substring(1));List<TreeVo>treePuls = PermissionDao.treePuls(sb .substring(1));//List<TreeVo>tree =permissionDao.tree(null, null);ResponseUtil.writeJson(resp,treePuls);} catch (Exception e) {e.printStackTrace();try {ResponseUtil.writeJson(resp, "0");} catch (IOException e1) {e1.printStackTrace();}}返回 null;}}
RolePermissionDao:
包 com.zking.dao;导入 java.util.List;导入 com.hpw.entity.RolePermission;导入 com.hpw.util.BaseDao;public classRolePermissionDao 扩展 BaseDao {/** * 通过users表的type字段查询卖家/买家对应的菜单ID * @param type * @return * @throws Exception */public List findRolePermission(int type ) throws Exception {String sql = "select * from t_easyui_role_Permission whererid = "+type;return super.executeQuery(sql, RolePermission.class, null);}}
效果展示:
买家 ID 登录
企业ID登录
此处退出。 重点是特权登录,但逻辑相对复杂
任何建议将不胜感激
评论前必须登录!
注册