<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

    主站蜘蛛池模板: 亚洲国产福利精品一区二区| 国产l精品国产亚洲区在线观看| 四虎亚洲精品高清在线观看| 100部毛片免费全部播放完整| 亚洲激情黄色小说| 91精品免费国产高清在线| 亚洲无成人网77777| 免费看国产精品3a黄的视频 | 67pao强力打造67194在线午夜亚洲 | 久久亚洲国产成人影院网站| 黄色网页在线免费观看| 亚洲国产美女精品久久久久∴| 精品一区二区三区免费| 精品亚洲A∨无码一区二区三区| 69av免费观看| 亚洲啪AV永久无码精品放毛片| 国产真人无遮挡作爱免费视频| 久久99精品免费一区二区| 久久亚洲国产视频| 97在线线免费观看视频在线观看| 亚洲av纯肉无码精品动漫| 久久精品国产精品亚洲| 99re视频精品全部免费| 亚洲va久久久久| 中文字幕日韩亚洲| 97青青草原国产免费观看| 亚洲色偷偷色噜噜狠狠99| 亚洲国产精品成人AV无码久久综合影院| 你是我的城池营垒免费看| 亚洲国产午夜精品理论片| 亚洲AⅤ优女AV综合久久久| 久久精品毛片免费观看| 亚洲成a∧人片在线观看无码| 亚洲综合色婷婷七月丁香| 午夜国产精品免费观看| 九九免费久久这里有精品23| 亚洲精品视频在线播放| 亚洲精品成人片在线观看| 99精品国产免费久久久久久下载| 国产精品免费视频观看拍拍| 亚洲免费福利在线视频|