jspカスタムラベル実装権限ブロック


最近のプロジェクトでは、ページ側で権限ブロック機能を実装する必要があります.具体的なニーズは、1人のユーザーがシステムにログインした後、ある機能を実行する権限があるかどうかを判断し、権限があれば、機能キーがそのユーザーに表示される.そうでなければ、見えません.この機能を実現するには、カスタムラベルが良い解決策に違いありません.具体的な方法は以下の通りです:(言語java、ページ実装jsp、開発ツールmyeclipse 6.5)
1、ラベルクラスを作成し、ラベルクラスは普通のjavaクラスであるが、このクラスはTagSupportクラスを継承しなければならない.
2、TLDファイルを作成し、式関数を定義する.
3、JSPページにインポートして使用する.
シナリオの説明:
1、ラベル類の作成
     
public class PermissionTag extends TagSupport {
   //  
   private String module;
   //       
   private String privilege;
 
   public String getModule() {
      return module;
   }
   public void setModule(String module) {
      this.module = module;
   }
   public String getPrivilege() {
      return privilege;
   }
   public void setPrivilege(String privilege) {
      this.privilege = privilege;
   }
   @Override
   public int doStartTag() throws JspException {
      boolean result = false;
      User user = WebUtil.getUser((HttpServletRequest) pageContext.getRequest());//WebUtil        ,         
      SystemPrivilege privilege = new SystemPrivilege(new SystemPrivilegePK(this.module,this.privilege));
      for(PrivilegeGroup privilegeGroup : user.getGroups()){//            
         privilegeGroup.getPrivileges().contains(privilege);//             
         result = true;
         break;
      }
      return result? EVAL_BODY_INCLUDE : SKIP_BODY;// :  EVAL_BODY_INCLUDE(    ); :  SKIP_BODY(       )
   }
}

 
2、クラスフォルダの下META-INFの下に置くTLDファイルを作成する.

<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>yuguan wuxipolice permission taglib</description><!-- --> <display-name>permission taglib</display-name> <tlib-version>1.0</tlib-version><!-- --> <short-name>yunguan</short-name> <!-- --> <uri>http://www.wuxipolice.cn/</uri> <!-- -->

<tag> <description> , , </description> <name>permission</name> <!-- --> <tag-class>com.yg.web.taglib.PermissionTag</tag-class> <!-- --> <body-content>JSP</body-content> <attribute> <description></description> <name>module</name> <!-- --> <required>true</required> <!-- --> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <description></description> <name>privilege</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>


 
3、JSPページにインポートして使用する.

<%@ taglib uri=http://www.wuxipolice.cn/WEB-INF/classes/META-INF/permission prefix="yg" %>

.........

<yg:permission module="department" privilege="update">       <a href="<html:rewrite action="/control/department/manage"/>?method=editDepartmentUI&departmentid=${entry.departmentid}">    <img src="/images/edit.gif" width="15" height="16" border="0"></a>    </yg:permission>

........