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 閱讀(255) 評論(0)  編輯  收藏 所屬分類: Struts2

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Hibernate

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丰城市| 新巴尔虎右旗| 循化| 清丰县| 南汇区| 青海省| 垣曲县| 山阳县| 安国市| 南郑县| 邵阳县| 永州市| 弋阳县| 石景山区| 三门县| 巴林左旗| 柯坪县| 苍南县| 瑞丽市| 栖霞市| 大埔区| 唐河县| 正定县| 北海市| 安泽县| 治县。| 大埔区| 肃南| 旌德县| 平阳县| 潞西市| 古交市| 句容市| 柞水县| 西吉县| 颍上县| 芷江| 武川县| 聂拉木县| 兴仁县| 漳平市|