<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 李云澤 閱讀(357) 評論(0)  編輯  收藏 所屬分類: J2EE

    主站蜘蛛池模板: 四虎成人免费大片在线| 最近的中文字幕大全免费8| 在线观看永久免费视频网站| 亚洲一区无码中文字幕乱码| 91精品国产免费久久国语蜜臀| 亚洲激情视频在线观看| 一区二区三区四区免费视频 | 99爱在线精品免费观看| 亚洲日本在线观看网址| 麻豆国产精品免费视频| 亚洲av成人综合网| 四虎免费在线观看| 人碰人碰人成人免费视频| 亚洲高清免费视频| 国产在线精品一区免费香蕉| 亚洲A∨无码一区二区三区| 99视频精品全部免费观看| 久久综合久久综合亚洲| 国产又粗又猛又爽又黄的免费视频 | 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 亚洲色欲色欲www| 高清国语自产拍免费视频国产| 看亚洲a级一级毛片| 亚洲日产韩国一二三四区| 日韩精品久久久久久免费| 亚洲中文字幕无码久久| 亚洲成?Ⅴ人在线观看无码| 中文字幕免费观看全部电影| 亚洲色欲色欲综合网站| 国内大片在线免费看| 一级毛片在线免费播放| 亚洲激情中文字幕| 在线免费观看一级片| 久久99精品免费一区二区| 亚洲一区二区免费视频| 免费在线黄色网址| 精品无码AV无码免费专区| 亚洲av无码一区二区三区天堂| 欧洲亚洲国产清在高| 无人在线观看免费高清视频| 免费一级毛suv好看的国产网站 |