午后星期午

          使用Spring mvc 利用java的反射技術,來掃描對應包下的注解請求url 統一保存在數據庫中

          僅對  @requestMapping("/xxxx/其他參數")  的格式
           package cc.wshao.springmvc.util;
          import java.io.File;
          import java.lang.reflect.Method;
          import java.net.URL;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;
          import cc.wshao.springmvc.service.RightService;
          public class RegRightUtils {
          private static final String rootUrl = "/spring";
          private static RightService rightService;
          public static void main(String[] args) throws Exception {
          ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            rightService = context.getBean(RightService.class);
          ClassLoader classLoader = RegRightUtils.class.getClassLoader();
          URL url = classLoader.getResource("com/er07/book/springmvc/controller");
          String rootPath = url.getPath().toString();
          File rootFile = new File(rootPath);
          File [] files = rootFile.listFiles();
          for(File f: files) {
          System.err.println(f.getName());
          String className = f.getName().substring(0, f.getName().indexOf(".class"));
          Class clazz = Class.forName("com.er07.book.springmvc.controller."+className);
          parseClass(clazz);  //解析  出url
          }
           
          }
          public  static void parseClass(Class clazz) {
           
          if(clazz.isAnnotationPresent(Controller.class)) {   //是否為一個controller ? 
          String classUrl ="";
           if(clazz.isAnnotationPresent(RequestMapping.class)) {
           RequestMapping requestMapping_clazz = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            classUrl = requestMapping_clazz.value()[0];
            
            if(classUrl.equals("/")) {  //如果是"/" 的話  制空    
            classUrl ="";
            }
           }
           Method [] ms = clazz.getDeclaredMethods();
           
           for(Method m :ms) {
           
           if(m.isAnnotationPresent(RequestMapping.class)) {
           RequestMapping requestMapping_method = m.getAnnotation(RequestMapping.class);
           String methodUrl = requestMapping_method.value()[0];
           
             int  index = methodUrl.indexOf("/");
             index = methodUrl.indexOf("/",index+1);
             if(index!=-1) {  //如果存在 則進行截取前面的url 
             
              methodUrl = methodUrl.substring(0, index);
             }
           String resultUrl = rootUrl+classUrl+methodUrl;
           rightService.appendRightByUrl(resultUrl);
           }
           }
          }
          }
          }
          //實現方法: 
          public void appendRightByUrl(String url) {
          //查詢 此url 在數據庫中是否存在  此權限
          String hql = "from Right where rightUrl =?";
          Right r = (Right) this.rightDao.uniqueResult(hql, url);
          if(r==null) { //不存在 , 插入數據庫中
          Right newRight = new Right();
          newRight.setRightUrl(url);
          this.saveOrUpdateRight(newRight);
          }
          }

          posted on 2014-03-25 14:54 午后星期午 閱讀(1253) 評論(0)  編輯  收藏 所屬分類: JavaEE

          主站蜘蛛池模板: 余江县| 新郑市| 鄂温| 青神县| 长武县| 井研县| 廉江市| 长宁区| 广水市| 孟村| 汾阳市| 慈利县| 中宁县| 博兴县| 那坡县| 潮安县| 米易县| 邵阳市| 柳州市| 原阳县| 得荣县| 甘孜县| 巨野县| 南昌县| 曲沃县| 巴林左旗| 宝坻区| 客服| 阿图什市| 华池县| 高安市| 灵台县| 小金县| 乌拉特中旗| 天全县| 印江| 马龙县| 呼和浩特市| 贵定县| 长白| 公主岭市|