ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自定义mvc注册登录及树形权限展示

2021-09-23 10:05:33  阅读:91  来源: 互联网

标签:return String 自定义 type 树形 mvc import com public


一、登录注册

1.根据MySQL中的表写实体类

package com.sjy.entity;

public class User {

	private long id;
	private String name;
	private String pwd;
	private int type;
	public long getId() {
		return id;
	}
	public void setId(int 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 User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(long id, String name, String pwd, int type) {
		super();
		this.id = id;
		this.name = name;
		this.pwd = pwd;
		this.type = type;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
}

2.写登录和注册的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"});
    }

3.子控制器内调用dao方法

package com.sjy.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sjy.dao.UserDao;
import com.sjy.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;

public class UserAction extends ActionSupport implements ModelDriver<User>{

	public User user=new User();
	public UserDao userDao=new UserDao();
	

	@Override
	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("user", 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 "Register";
		}
		return "toLogin";
	}
}

u为null时即登录失败,重新跳到登录界面

登录成功后将用户放入session里面,方便之后用到 

 4.配置mvc文件

<action path="/user" type="com.sjy.web.UserAction"> 
         <forward name="toLogin" path="/login.jsp" redirect="false" /> 
         <forward name="Register" path="/register.jsp" redirect="true" /> 
         <forward name="main" path="/bg/mainTemp.jsp" redirect="false" /> 
         </action>

 二、树形权限展示

1.卖家和买家需要展示不同的功能,根据表关系可分析到

type为1是管理员,2为普通用户 

2.t_easyui_role_permission表中有1和2对应的功能id

3.先建立实体类 roleprimission

package com.sjy.entity;

public class RolePermission {

	public RolePermission() {
		super();
	}
	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 + "]";
	}
	
}

 4.根据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);
    }  

type为之前session里面的type

5.用,将rid拼接起来

int type = user.getType();
            List<RolePermission> findRolePermission = rolepd.findRolePermission(type);
            
            StringBuffer s=new StringBuffer();
            for (RolePermission f : findRolePermission) {
                s.append(",").append(f.getPid());
            }
            String substring = s.substring(1); 

 s.substring(1)是将第一个,去掉

6.通过帮助类将平级数据变成有阶级关系的数据

public List<TreeVo<Permission>> tree(String ids) throws Exception {
		List<Permission> list = this.listPulas(ids);
		List<TreeVo<Permission>> listT=new ArrayList<TreeVo<Permission>>();
		for (Permission p : list) {
			TreeVo<Permission> vo=new TreeVo<Permission>();
			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);
			listT.add(vo);
		}
		return BuildTree.buildList(listT, "0");
	}

7.转化成json字符串

package com.sjy.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sjy.dao.PermissionDao;
import com.sjy.dao.RolePermissionDao;
import com.sjy.entity.Permission;
import com.sjy.entity.RolePermission;
import com.sjy.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>{

	public Permission permission=new Permission();
	public PermissionDao permissionDao=new PermissionDao();
	RolePermissionDao rolepd=new RolePermissionDao();

	@Override
	public Permission getModel() {
		return permission;
	}
	
	public String tree(HttpServletRequest req, HttpServletResponse resp) {
		try {
			User user =(User)req.getSession().getAttribute("user");
			if(user==null) {
				return "toLogin";
			}
			int type = user.getType();
			List<RolePermission> findRolePermission = rolepd.findRolePermission(type);
			
			StringBuffer s=new StringBuffer();
			for (RolePermission f : findRolePermission) {
				s.append(",").append(f.getPid());
			}
			String substring = s.substring(1);
			List<TreeVo<Permission>> tree = permissionDao.tree(substring);
		
			//List<TreeVo<Permission>> tree = permissionDao.tree(permission);
			ResponseUtil.writeJson(resp, tree);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}

8.配置mvc文件

<action path="/permission" type="com.sjy.web.PermissionAction"> 
 	    <forward name="toLogin" path="/login.jsp" redirect="false" /> 
 	    </action>

9.js内配置路径

$(function(){
    $("#bookMenus").tree({
        url:$("#ctx").val()+"/permission.action?methodName=tree",
        
    })
    
})

就可以实现啦!!

标签:return,String,自定义,type,树形,mvc,import,com,public
来源: https://blog.csdn.net/weixin_52639224/article/details/120428254

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有