⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rightroleaction.java

📁 java电信经典-学习例子源代码
💻 JAVA
字号:
package netctoss.rights.actions;

import global.ModuleMenu;
import global.PageParameters;
import global.RightMenu;
import global.SortMenu;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

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

import netctoss.dao.AdminsDAO;
import netctoss.dao.ModulesDAO;
import netctoss.dao.OperationsDAO;
import netctoss.dao.RightsDAO;
import netctoss.dao.RolesDAO;
import netctoss.entities.Adminroles;
import netctoss.entities.Admins;
import netctoss.entities.Modules;
import netctoss.entities.Rights;
import netctoss.entities.Rolerights;
import netctoss.rights.forms.RightCondForm;
import netctoss.rights.forms.RoleCondForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Query;

public class RightRoleAction extends DispatchAction {
	//查询指定模块下的操作权限
	private Modules getOperationMenuByRight(Integer  id){
		ModulesDAO dao= new ModulesDAO();
		dao.getSession().flush();
		return dao.findById(id);
	}
	//index负责权限管理主页面逻辑
	public ActionForward index(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//根据传递的参数,得到模块ID,并保存模块ID到session
		String moduleid=request.getParameter("moduleid");
		if(moduleid!=null){
			Modules tmpmodule=getOperationMenuByRight(new Integer(moduleid));
			ModuleMenu module=new ModuleMenu();
			module.setId(tmpmodule.getId());
			module.setName(tmpmodule.getName());
			module.setUrl(tmpmodule.getUrl());
			request.getSession().setAttribute("module", module);
			
			String adminid=request.getSession().getAttribute("adminid").toString();
			//得到该用户的角色->权限->过滤得到
			AdminsDAO daoadmin=new AdminsDAO();
			//得到管理员
			Admins admin=daoadmin.findById(new Integer(adminid));
			//存放权限,便于过滤
			List<Rights> listallright=new Vector<Rights>();
			//得到用户角色集合
			Set  setadminroles=admin.getAdminroles();
			Iterator  it=setadminroles.iterator();
			//循环找出角色的所有权限
			while(it.hasNext()){
				Adminroles adminrole=(Adminroles)it.next();
				Set  setroleright=adminrole.getRoleid().getRolerights();
				Iterator itroleright=setroleright.iterator();
				while(itroleright.hasNext()){
					Rolerights   rr=(Rolerights)itroleright.next();
					if(rr.getRightid().getModuleid().getId().equals(new Integer(moduleid))){
						listallright.add(rr.getRightid());
					}
					
				}
			}
			//这里过滤重复的权限。
			Set setrights=new HashSet();
			for(Object obj:listallright){
				setrights.add(obj);
			}
			//排序
			RightMenu[]  rm=new RightMenu[setrights.size()];
			
			Object[] obj=setrights.toArray();
			for(int i=0;i<obj.length;i++){
				rm[i]=new RightMenu();
				rm[i].setId(((Rights)obj[i]).getId());
				rm[i].setName(((Rights)obj[i]).getName());
				rm[i].setUrl(((Rights)obj[i]).getUrl());
			}
			//权限排序。
			Arrays.sort(rm,new SortMenu());
			//设置权限菜单到session,便于下步导航			
			request.getSession().setAttribute("menu", rm);
		}
		else{
			return mapping.findForward("back");
		}
		//根据提交的方法确定数据处理
		return mapping.findForward("goindex");
	}
	//browseRight负责权限的查询浏览
	public ActionForward browseRight(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		queryRight("browseRight",mapping,form,request,response);
		return mapping.findForward("gobrowseright");
	}
	//browseRole负责角色的查询浏览
	public ActionForward browseRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		queryRole("browseRole",mapping,form,request,response);
		return mapping.findForward("gobrowserole");
	}
	//deleteRight负责权限删除
	public ActionForward deleteRight(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		queryRight("deleteRight",mapping,form,request,response);
		return mapping.findForward("godeleteright");
	}
	//deleteRight负责删除权限	
	public ActionForward deleteRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		queryRole("deleteRight",mapping,form,request,response);
		return mapping.findForward("godeleterole");
	}
	//updateRight负责权限修改
	public ActionForward updateRight(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		queryRight("updateRight",mapping,form,request,response);
		return mapping.findForward("goupdateright");
	}
	//updateRole负责角色修改
	public ActionForward updateRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		queryRole("updateRole",mapping,form,request,response);
		return mapping.findForward("goupdaterole");
	}
	//addnewRight负责权限增加
	public ActionForward addnewRight(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//直接显示权限输入界面,这里牵涉多表查询。
		//查询所有的模块与所有的操作
		ModulesDAO daomod=new ModulesDAO();
		List lstmod=daomod.findAll();
		OperationsDAO daoper=new OperationsDAO();
		List lstoper=daoper.findAll();
		request.setAttribute("lstmod", lstmod);
		request.setAttribute("lstoper", lstoper);
		
		return mapping.findForward("goaddnewright");
	}
	public ActionForward addnewRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//直接显示权限输入界面,这里牵涉多表查询。
		//查询所有的权限
		
		RightsDAO dao=new RightsDAO();
		List lstright=dao.findAll();
		request.setAttribute("lstright", lstright);
		return mapping.findForward("goaddnewrole");
	}
	//公共处理部分==========================================
	//权限的查询
	public void queryRight(String method,ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//权限菜单由index处理。
		//这里只负责浏览查询的业务逻辑
		//显示所有权限列表,负责分页数据,负责查询按钮的数据
		//1查询所有权限。
		//得到查询条件:模块名称,操作名称,当前页数
		RightCondForm  fm=(RightCondForm)form;
		//当前页数
		int currentpage=1;
		String hql="";
		
		if(fm.getPage()==0)//说明是提交查询
		{			
			currentpage=1;
			//重新查询数据
			String modulename=fm.getModulename();
			String operationname=fm.getOperationname();
			//建立DAO对象进行数据查询
			
			//所有权限		
			hql="from netctoss.entities.Rights as rights  ";
			hql+="where rights.moduleid.name like '%"+modulename+"%' ";
			hql+="and  ";
			hql+="rights.name like '%"+operationname+"%'";
			request.getSession().setAttribute("hql", hql);
			request.getSession().setAttribute("form", fm);
		}
		else{//说明是分页查询
			currentpage=fm.getPage();
			hql=request.getSession().getAttribute("hql").toString();			
		}
		RightsDAO  dao=new  RightsDAO();
		Query query=dao.getSession().createQuery(hql);
			
		List  listrights=query.list();
		
		

		//分页导航条
		int totals=listrights.size();
		int totalpages=totals%PageParameters.ITEMS_EACH_PAGE==0?totals/PageParameters.ITEMS_EACH_PAGE:totals/PageParameters.ITEMS_EACH_PAGE +1;
		String pagenav=PageParameters.pageNavigator(totalpages, currentpage, request.getContextPath()+ request.getServletPath()+"?method="+method);
		//pagenav=response.encodeRedirectURL(pagenav);
		request.setAttribute("pagenav", pagenav);//设置分页导航数据
		//过滤数据只获取满足当前条件的数据
		
		
		query.setFirstResult((currentpage-1)*PageParameters.ITEMS_EACH_PAGE);
		query.setMaxResults(PageParameters.ITEMS_EACH_PAGE);
		listrights=query.list();		
		request.setAttribute("listrights", listrights);
		
	}
	//公共处理部分
	//角色的查询
	/*该部分牵涉的标有两张,角色表与角色权限分配表*/
	public void queryRole(String method,ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//权限菜单由index处理。
		//这里只负责浏览查询的业务逻辑
		//显示所有权限列表,负责分页数据,负责查询按钮的数据
		//1查询所有角色。
		//得到查询条件:角色名称,当前页数
		RoleCondForm  fm=(RoleCondForm)form;
		//当前页数
		int currentpage=1;
		String hql="";		
		if(fm.getPage()==0)//说明是提交查询
		{			
			currentpage=1;
			//重新查询数据
			String rolename=fm.getRolename();			
			//建立DAO对象进行数据查询			
			//所有权限		
			hql="from netctoss.entities.Roles as roles  ";
			hql+="where ";
			hql+="roles.name like '%"+rolename+"%'";
			request.getSession().setAttribute("hql", hql);
			//表单临时存放,注意这个表单中数据名是否与前面模块中冲突
			request.getSession().setAttribute("form", fm);
		}
		else{//说明是分页查询
			currentpage=fm.getPage();
			hql=request.getSession().getAttribute("hql").toString();			
		}
		RolesDAO  dao=new  RolesDAO();
		Query query=dao.getSession().createQuery(hql);			
		List  listroles=query.list();
		
		//System.out.println();
		

		//分页导航条
		int totals=listroles.size();
		int totalpages=totals%PageParameters.ITEMS_EACH_PAGE==0?totals/PageParameters.ITEMS_EACH_PAGE:totals/PageParameters.ITEMS_EACH_PAGE +1;
		String pagenav=PageParameters.pageNavigator(
				totalpages, //总页数
				currentpage, //当前页数
				//导航的目标url
				request.getContextPath()+ request.getServletPath()+"?method="+method);
		//pagenav=response.encodeRedirectURL(pagenav);
		request.setAttribute("pagenav", pagenav);//设置分页导航数据
		//过滤数据只获取满足当前条件的数据		
		query.setFirstResult((currentpage-1)*PageParameters.ITEMS_EACH_PAGE);
		query.setMaxResults(PageParameters.ITEMS_EACH_PAGE);
		listroles=query.list();
		request.setAttribute("listroles", listroles);		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -