实战Struts-Menu(4)

实战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 >

你可能感兴趣的:(实战Struts-Menu(4))