gdufo

           

          實戰Struts-Menu(4)

          /**
               * 從數據庫中讀取菜單配置信息
               * 
               * 
          @return
               
          */
              
          private Map[] getMenuComponents() {
                  ArrayList list 
          = new ArrayList();
                  Connection conn 
          = null;
                  PreparedStatement pstmt 
          = null;
                  ResultSet rest 
          = null;
                  String sql 
          = "select name,parent_name,title,location,description from menu_item order by id";
                  
          try {
                      Class.forName(
          "com.mysql.jdbc.Driver").newInstance();
                      conn 
          = DriverManager.getConnection("jdbc:mysql://localhost/myexamples?user=root&password=mywangya&useUnicode=true&characterEncoding=UTF-8");
                      pstmt 
          = conn.prepareStatement(sql);
                      rest 
          = pstmt.executeQuery();
                      
          while (rest.next()) {
                          
          int i = 1;
                          HashMap map 
          = new HashMap();
                          map.put(
          "name", rest.getString(i++));
                          map.put(
          "parent_name", rest.getString(i++));
                          map.put(
          "title", rest.getString(i++));
                          map.put(
          "location", rest.getString(i++));
                          map.put(
          "description", rest.getString(i++));
                          list.add(map);
                      }
                  } 
          catch (SQLException ex) {
                      ex.printStackTrace();
                  } 
          catch (InstantiationException e) {
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  } 
          finally {
                      
          try {
                          
          if (null!=rest) rest.close();
                          
          if (null!=pstmt) pstmt.close();
                          
          if (null!=conn) conn.close();
                      } 
          catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
                  
                  
          return (Map[]) list.toArray(new HashMap[0]);
              }
              
              
          /**
               * 構造菜單權限
               * 
               * 
          @param request
               
          */
              
          private void buildMenuPermissions(HttpServletRequest request) {
                  PermissionsAdapter permession 
          = new PermissionsAdapter() {
                      
          public boolean isAllowed(MenuComponent menu) {
                          
          // 名稱等于StandaloneMenu的菜單不顯示
                          return !"StandaloneMenu".equalsIgnoreCase(menu.getName());
                      }
                  };
                  request.setAttribute(
          "examplesPermession", permession);
              }

              
          /**
               * 構造菜單顯示標題
               * 
               * 
          @param request
               
          */
              
          private void buildMenuResourceBundle(HttpServletRequest request) {
                  MenuResourceBundle resourceBundle 
          = new MenuResourceBundle();
                  request.setAttribute(
          "examplesBundle", resourceBundle);
              }
              
              
          /**
               * MenuResourceBundle樹狀菜單國際語言顯示
               * 
               * 
          @author wenbin.zhang
               *  
               
          */
              
          class MenuResourceBundle extends ListResourceBundle {
                  
          private ArrayList list = new ArrayList();

                  
          public MenuResourceBundle() {
                      Connection conn 
          = null;
                      PreparedStatement pstmt 
          = null;
                      ResultSet rest 
          = null;
                      String sql 
          = "select title,titleCN from menu_item order by id";
                      
          try {
                          Class.forName(
          "com.mysql.jdbc.Driver").newInstance();
                          conn 
          = DriverManager.getConnection("jdbc:mysql://localhost/myexamples?user=root&password=mywangya&useUnicode=true&characterEncoding=UTF-8");
                          pstmt 
          = conn.prepareStatement(sql);
                          rest 
          = pstmt.executeQuery();
                          
          while (rest.next()) {
                              
          int i = 1;
                              String[] message 
          = new String[2];
                              message[
          0= rest.getString(i++);
                              
          try {
                                  message[
          1= new String(rest.getString(i++).getBytes("latin1"), "gbk");
                              } 
          catch (UnsupportedEncodingException e) {
                                  e.printStackTrace();
                              }
                              
          if (message[0!= null && message[1!= null) {
                                  list.add(message);
                              }
                          }
                      } 
          catch (SQLException ex) {
                          ex.printStackTrace();
                      } 
          catch (InstantiationException e) {
                          e.printStackTrace();
                      } 
          catch (IllegalAccessException e) {
                          e.printStackTrace();
                      } 
          catch (ClassNotFoundException e) {
                          e.printStackTrace();
                      } 
          finally {
                          
          try {
                              
          if (null!=rest) rest.close();
                              
          if (null!=pstmt) pstmt.close();
                              
          if (null!=conn) conn.close();
                          } 
          catch (SQLException e) {
                              e.printStackTrace();
                          }
                      }

                  }

                  
          public Object[][] getContents() {
                      
          return (String[][]) list.toArray(new String[0][0]);
                  }
              }
          }

          > 將struts-config.xml文件的<action-mappings />部分修改為:
          <action-mappings>
            
          <action path="/menuAction" type="cn.appex.menu.MenuAction" >
              
          <forward name="success" path="/struts-menu/dynamic-menu.jsp" />
            
          </action>
          </action-mappings>

          posted on 2008-08-05 15:05 gdufo 閱讀(261) 評論(0)  編輯  收藏 所屬分類: Struts2

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Hibernate

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 治县。| 杂多县| 焉耆| 石屏县| 龙里县| 菏泽市| 南涧| 扬中市| 洪湖市| 名山县| 丁青县| 杭州市| 邵武市| 平邑县| 景泰县| 常州市| 富阳市| 汽车| 永和县| 平利县| 黔南| 阿图什市| 鲜城| 青河县| 南召县| 武汉市| 香港| 沙雅县| 西峡县| 改则县| 射阳县| 西昌市| 朝阳县| 汕尾市| 临漳县| 密山市| 东乌珠穆沁旗| 衡山县| 民权县| 庆云县| 齐河县|