<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我思故我強

    系統權限解決方案(轉載)


    每個軟件中都有權限這個功能,搞了個通過tag實現的方法,復用性很強,


    psy-operation.tld

    Xml代碼
    <?xml version="1.0" encoding="UTF-8" ?>?
    <taglib xmlns="??? xmlns:xsi="??? xsi:schemaLocation="http://www.blog.com.cn/http://java.sun.com/xml/ns/j2ee??? version="? <description>?
    ?psychcn 標記庫 1.0??
    ? </description>?
    ? <tlib-version>1.0</tlib-version>?
    ? <short-name>psydict</short-name>?
    ? <uri>http://www.psychcn.com/taglibs</uri>?
    ??? <tag>?
    ??? <name>op</name>?
    ??? <description>權限標簽</description>?
    ??? <tag-class>com.psychcn.web.tags.OperationTag</tag-class>?
    ?<body-content>scriptless</body-content>?
    ??????
    ??? <attribute>?
    ?????? <name>code</name>?
    ?????? <required>true</required>?
    ?????? <rtexprvalue>true</rtexprvalue>?
    ??? </attribute>?
    ??? <attribute>?
    ?????? <name>opset</name>?
    ?????? <required>false</required>?
    ?????? <rtexprvalue>true</rtexprvalue>?
    ??? </attribute>??????
    ? </tag>?
    </taglib>?

    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib xmlns="
    ??? xmlns:xsi="??? xsi:schemaLocation="http://www.blog.com.cn/http://java.sun.com/xml/ns/j2ee??? version="? <description>
    ?psychcn 標記庫 1.0
    ? </description>
    ? <tlib-version>1.0</tlib-version>
    ? <short-name>psydict</short-name>
    ? <uri>http://www.psychcn.com/taglibs</uri>
    ??? <tag>
    ??? <name>op</name>
    ??? <description>權限標簽</description>
    ??? <tag-class>com.psychcn.web.tags.OperationTag</tag-class>
    ?<body-content>scriptless</body-content>
    ???
    ??? <attribute>
    ?????? <name>code</name>
    ?????? <required>true</required>
    ?????? <rtexprvalue>true</rtexprvalue>
    ??? </attribute>
    ??? <attribute>
    ?????? <name>opset</name>
    ?????? <required>false</required>
    ?????? <rtexprvalue>true</rtexprvalue>
    ??? </attribute>???
    ? </tag>
    </taglib>

    OperationTag.java

    Java代碼
    import javax.servlet.jsp.JspException;??
    import javax.servlet.jsp.tagext.SimpleTagSupport;??
    ?
    import java.io.IOException;??
    import java.util.*;??
    ?
    public class OperationTag extends SimpleTagSupport {??
    ?private Set operation_set;??
    ?private String default_operation_set_name = "ops";??
    ?private String code;??
    ???
    ?public void setCode(String code) {??
    ? this.code = code;??
    ?}??
    ?public void setOpset(Set operation_set) {??
    ? this.operation_set = operation_set;??
    ?}??
    ?public void setOpsetName(String name) {??
    ? this.default_operation_set_name= name;??
    ?}??
    ???
    ?public void doTag() throws JspException, IOException{??
    ? //session中沒有設置權限HashSet,給默認值??
    ? if (operation_set==null) {??
    ?? Object o = this.getJspContext().findAttribute(default_operation_set_name);??
    ?? if (o instanceof Set)??
    ??? operation_set = (Set)o;??
    ? }??
    ????
    ? if (code == null || operation_set == null)??
    ?? throw new JspException("標簽屬性無效,無法執行!");??
    ????
    ? //這里支持多個code,用','分割,有一個符合條件就輸出,全部不滿足則不輸出(注意不能有空格,區分大小寫)??
    ? String[] codes = code.split(",");??
    ? for (String s : codes) {??
    ?? if (operation_set.contains(s)) {??
    ??? this.getJspBody().invoke(this.getJspContext().getOut());??
    ??? return;??
    ?? }??
    ? }??
    ?}??
    }?

    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;

    import java.io.IOException;
    import java.util.*;

    public class OperationTag extends SimpleTagSupport {
    ?private Set operation_set;
    ?private String default_operation_set_name = "ops";
    ?private String code;
    ?
    ?public void setCode(String code) {
    ? this.code = code;
    ?}
    ?public void setOpset(Set operation_set) {
    ? this.operation_set = operation_set;
    ?}
    ?public void setOpsetName(String name) {
    ? this.default_operation_set_name= name;
    ?}
    ?
    ?public void doTag() throws JspException, IOException{
    ? //session中沒有設置權限HashSet,給默認值
    ? if (operation_set==null) {
    ?? Object o = this.getJspContext().findAttribute(default_operation_set_name);
    ?? if (o instanceof Set)
    ??? operation_set = (Set)o;
    ? }
    ?
    ? if (code == null || operation_set == null)
    ?? throw new JspException("標簽屬性無效,無法執行!");
    ?
    ? //這里支持多個code,用','分割,有一個符合條件就輸出,全部不滿足則不輸出(注意不能有空格,區分大小寫)
    ? String[] codes = code.split(",");
    ? for (String s : codes) {
    ?? if (operation_set.contains(s)) {
    ??? this.getJspBody().invoke(this.getJspContext().getOut());
    ??? return;
    ?? }
    ? }
    ?}
    }
    ?

    ?

    底層查找權限接口:

    OperationService.java

    Java代碼
    public java.util.HashSet<String> findByUserId(String userId) throws Exception;??
    實現接口類:(//通過USERID找到對應的operation的code)??
    ?
    OperationServiceImpImp.java??
    ?
    ?public java.util.HashSet<String> findByUserId(String userId) throws Exception{??
    ? Session s = getSession();??
    ? Transaction tx = s.beginTransaction();??
    ????
    ? String sql = "select DISTINCT o.code from users u " +??
    ????? "inner join groupmember gm on u.userId=gm.user_Id " +???
    ????? "inner join groupacl ga on gm.group_id=ga.group_id " +??
    ????? "inner join operation o on ga.op_id = o.id " +??
    ????? "where u.userId=?";??
    ? Query q = s.createSQLQuery(sql).setString(0,userId);??
    ? List<Object> ls = q.list();??
    ? HashSet ops = new HashSet();??
    ? for(Object object : ls){??
    ?? ops.add(object);??
    ? }??
    ? tx.commit();??
    ? releaseSession(s);??
    ????
    ? return ops;??
    ?}?

    public java.util.HashSet<String> findByUserId(String userId) throws Exception;
    實現接口類:(//通過USERID找到對應的operation的code)

    OperationServiceImpImp.java

    ?public java.util.HashSet<String> findByUserId(String userId) throws Exception{
    ? Session s = getSession();
    ? Transaction tx = s.beginTransaction();
    ?
    ? String sql = "select DISTINCT o.code from users u " +
    ????? "inner join groupmember gm on u.userId=gm.user_Id " +
    ????? "inner join groupacl ga on gm.group_id=ga.group_id " +
    ????? "inner join operation o on ga.op_id = o.id " +
    ????? "where u.userId=?";
    ? Query q = s.createSQLQuery(sql).setString(0,userId);
    ? List<Object> ls = q.list();
    ? HashSet ops = new HashSet();
    ? for(Object object : ls){
    ?? ops.add(object);
    ? }
    ? tx.commit();
    ? releaseSession(s);
    ?
    ? return ops;
    ?}

    這樣,在用戶登錄時,可以把該用戶的權限HashSet裝載到Session中

    ?

    //把當前用戶的權限添加到HashSet
    ??

    Java代碼
    HashSet ops = AppResource.operationService.findByUserId(user.getUserId());??
    session.setAttribute("ops",ops);?

    HashSet ops = AppResource.operationService.findByUserId(user.getUserId());
    session.setAttribute("ops",ops);

    最后,在JSP中就可以簡單的使用標簽來判斷有沒有某個權限,沒有則不顯示

    Xml代碼
    <%@ taglib prefix="psy" uri="
    <psy:op code="Finance_Payment">看你有沒有權限讓我顯示</psy:op>?

    <%@ taglib prefix="psy" uri="<psy:op code="Finance_Payment">看你有沒有權限讓我顯示</psy:op>

    ?

    OK!可以根據需要修改。

    posted on 2009-01-04 15:40 李云澤 閱讀(350) 評論(0)  編輯  收藏 所屬分類: J2EE

    主站蜘蛛池模板: 18禁超污无遮挡无码免费网站国产 | 日韩av无码免费播放| 国产一区在线观看免费| 亚洲精品精华液一区二区| 手机在线毛片免费播放| 色窝窝亚洲AV网在线观看| 国产性生交xxxxx免费| 在线播放免费人成视频网站| 免费乱理伦在线播放| 一级毛片免费在线播放| 国精无码欧精品亚洲一区| 97国产在线公开免费观看| 亚洲成人免费在线观看| 在线播放免费播放av片| 国产亚洲视频在线| 亚洲综合精品香蕉久久网| 日本视频免费高清一本18| 亚洲国产综合在线| 日韩在线视频免费看| 久青草国产免费观看| 亚洲AV天天做在线观看| 国产一卡2卡3卡4卡2021免费观看| 亚洲一级毛片中文字幕| 国产真人无遮挡作爱免费视频 | 亚洲av无码成人影院一区| 亚洲日韩中文在线精品第一| 国产精品免费无遮挡无码永久视频| 亚洲免费一级视频| 国产免费拔擦拔擦8x| 野花香高清在线观看视频播放免费| 亚洲女人初试黑人巨高清| 丁香亚洲综合五月天婷婷| 久久免费看少妇高潮V片特黄| 亚洲综合中文字幕无线码| 亚洲一区无码精品色| 久久精品一本到99热免费| 亚洲AV无码一区二区一二区| 久久精品国产亚洲av成人| 日本成人免费在线| 最近中文字幕高清免费中文字幕mv | 久久久久亚洲AV无码麻豆|