示例:用自定义注解实现对权限的控制。如果只有读权限就只可见页面上查询部分,如果有写权限或全部权限就不但能操作查询部分还能操作修改部分。(查询部分和修改部分用文字代替,但只要在页面可见就表示可以操作)
首先是定义注解类和使用这个自定义注解的类:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LevelAnnotation { public enum AuthLevel{read,write,all} public AuthLevel getAuthLevel() default AuthLevel.read; public String memberName() default ""; }
import com.taobao.member.web.module.screen.prop.LevelAnnotation.AuthLevel; public class MemberInfo { @LevelAnnotation(memberName="") //使用注解的参数memberName如果为空,就会执行下面用随机数形成权限等级的部分;如果由@LevelAnnotation(memberName="")改成@LevelAnnotation(memberName="zirou"),即memberName为"zirou"就赋给全部权限,只会出现(页面三)
public AuthLevel getMemberLevel(String memberName) { AuthLevel level = null; if("zirou".equals(memberName)){ level = AuthLevel.all; }else{ int i = 1+(int)(Math.random()*3); if(i==1){ level = AuthLevel.read; }else if(i==2){ level = AuthLevel.write; }else if(i==3){ level = AuthLevel.all; } } return level; } }
然后是这个自定义注解的处理类:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import com.taobao.member.web.module.screen.prop.LevelAnnotation.AuthLevel; public class AnnotationParse { public AuthLevel parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{ Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{}); for(Method method : clazz.getDeclaredMethods()){ LevelAnnotation levelAnnotation = method.getAnnotation(LevelAnnotation.class); if(levelAnnotation != null){ AuthLevel level = (AuthLevel) method.invoke(obj, levelAnnotation.memberName()); return level; } } return null; } }
展现效果最直接的方式就是用页面展现了,眼见为实嘛:
public class Screen { public void execute(Context context) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { AnnotationParse annotationParse = new AnnotationParse(); AuthLevel auth = annotationParse.parseMethod(MemberInfo.class); if(auth.equals(AuthLevel.read)){ context.put("memberLevel", Integer.toString(1)); }else if(auth.equals(AuthLevel.write)){ context.put("memberLevel", Integer.toString(2)); }else if(auth.equals(AuthLevel.all)){ context.put("memberLevel", Integer.toString(3)); } } }
<form action="" method="post" enctype="multipart/form-data"> <table width="100%" align="center" border="1" cellpadding="12" cellspacing="0" bgcolor="white"> memberLevel=$memberLevel <br/> --------------------------------------------- #if("$memberLevel"=="1"||"$memberLevel"=="2"||"$memberLevel"=="3") <div class="bd"> <h2>查询部分</h2> </div> --------------------------------------------- #end #if("$memberLevel"=="2"||"$memberLevel"=="3") <div class="bd"> <h2>修改部分</h2> </div> #end </table> </form>
页面效果是这样滴:
(页面二)等级是2有读、写权限的如下图
(页面三)等级是3有全部权限的如下图