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

📄 xcheckbox.java

📁 Struts_Spring_Hibernate实现的基于RBAC的权限管理系统
💻 JAVA
字号:
package com.xaccp.struts.taglib;

import java.io.IOException;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.xaccp.biz.po.Power;
import com.xaccp.biz.po.Role;
import com.xaccp.biz.po.RolePower;
import com.xaccp.biz.po.User;
import com.xaccp.biz.po.UserPower;

public class XCheckBox extends TagSupport {
	
	private static int count = 1;
	private String text;
	private String name;
	private String currentUser;
	private String currentRole;
	private int powerId;
	
	public String getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(String currentUser) {
		this.currentUser = currentUser;
	}

	public int getPowerId() {
		return powerId;
	}

	public void setPowerId(int powerId) {
		this.powerId = powerId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	private synchronized static String getUUID() {
		String id = "xcb" + Long.toHexString(System.currentTimeMillis());
		id = id + (count++);
		return id;
	}
	
	public int doEndTag() throws JspException {
		String id = XCheckBox.getUUID();
		
		JspWriter out = super.pageContext.getOut();
		
		int defaultValue = getDefaultValue();
		try {
			out.print("<img style=\"cursor:hand\" src=\"images/state" + defaultValue + ".gif\" "); 
			out.print("onclick=\"h=document.getElementById('" + id + "');");
			out.print("this.src='images/state' + ((h.value-0+1) % 3) +'.gif';");
			out.print("h.value=((h.value-0+1) % 3)\"> ");
			out.print(text);
			out.print("<input type=hidden name=" + name + " value="+ defaultValue +" id="+ id +">");
		} catch (IOException e) {
			e.printStackTrace();
		}	
		return super.doEndTag();
	}

	private Object getFromScope(String key) {
		if (key==null) return null;
		Object obj = pageContext.getAttribute(key);
		if (obj==null) {
			obj = pageContext.getRequest().getAttribute(key);
		}
		if (obj==null) {
			obj = pageContext.getSession().getAttribute(key);
		}
		if (obj==null) {
			obj = pageContext.getServletContext().getAttribute(key);
		}
		return obj;
	}
	
	private int getDefaultValue() {
		if (powerId==0) return 2;
		
		User user = (User) getFromScope(currentUser);
		Role role = (Role) getFromScope(currentRole);

		//按照当前用户的权限判断
		if (user!=null) {
			Set up = user.getTblUserPowers();
			for (Object obj : up) {
				Power p = ((UserPower)obj).getTblPower();
				if (powerId==p.getId()) {
					return ((UserPower)obj).getState();
				}
			}
			return 2;
		}
		
		//按照当前角色的权限判断
		if (role!=null) {
			Set rp = role.getTblRolePowers();
			for (Object obj : rp) {
				Power p = ((RolePower)obj).getTblPower();
				if (powerId==p.getId()) {
					return ((RolePower)obj).getState();
				}
			}
			return 2;
		}
		
		return 2;
	}

	public String getCurrentRole() {
		return currentRole;
	}

	public void setCurrentRole(String currentRole) {
		this.currentRole = currentRole;
	}
}

⌨️ 快捷键说明

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