实战Struts-Menu(3)

实战Struts-Menu(3)
 插入测试数据:
INSERT INTO menu_item
    (id
,  parent_name ,  name ,  title ,  titleCN ,  location)
VALUES
    (
1 , null , 'DatabaseMenu' , 'Database Menu' , '数据库动态菜单' , null) ,
    (
2 , 'DatabaseMenu' , 'Yahoo' , 'Yahoo Mail' , '雅虎邮件' , 'http://mail.yahoo.com') ,
    (
3 , 'DatabaseMenu' , 'JavaBlogs' , 'JavaBlogs' , 'Java博客' , 'http://javablogs.com') ,
    (
4 , null , 'StandaloneMenu' , 'Standalone Menu' , '独立的菜单' , 'http://www.sohu.com')
> 将数据库驱动程序放到lib目录中,并加入到Build Path,如MySQL的数据库驱动mysql-connector-java-3.1.8-bin.jar,解压缩commons- collections-3.1.zip,将commons-collections-3.1.jar释放到lib目录,并加入Build Path
  > 新建一个Struts的Action,代码如下:

package  cn.appex.menu;

import  java.io.UnsupportedEncodingException;
import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.ListResourceBundle;
import  java.util.Map;

import  com.mysql.jdbc.Driver;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;

import  net.sf.navigator.menu.MenuComponent;
import  net.sf.navigator.menu.MenuRepository;
import  net.sf.navigator.menu.PermissionsAdapter;

import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

public   class  MenuAction  extends  Action {
    
public  ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws  Exception {
        buildMenuRepository(request);
        buildMenuResourceBundle(request);
        buildMenuPermissions(request);
        
return  (mapping.findForward( " success " ));
    }

    
/**
     * 创建菜单数据结构
     * 
     * 
@param  request
     
*/
    
private   void  buildMenuRepository(HttpServletRequest request) {
        MenuRepository repository 
=   new  MenuRepository();
        
//  Get the repository from the application scope - and copy the
        
//  DisplayerMappings from it.
        MenuRepository defaultRepository  =  (MenuRepository) request
                .getSession().getServletContext().getAttribute(
                        MenuRepository.MENU_REPOSITORY_KEY);
        repository.setDisplayers(defaultRepository.getDisplayers());

        Map[] menus 
=  getMenuComponents();
        
for  ( int  i = 0 ; i  <  menus.length; i ++ ) {
            MenuComponent mc 
=   new  MenuComponent();
            Map row 
=  menus[i];
            String name 
=  (String) row.get( " name " );
            mc.setName(name);
            String parent 
=  (String) row.get( " parent_name " );
            System.out.println(name 
+   " , parent is:  "   +  parent);
            
if  (parent  !=   null ) {
                MenuComponent parentMenu 
=  repository.getMenu(parent);
                
if  (parentMenu  ==   null ) {
                    System.out.println(
" parentMenu ' "   +  parent  +   " ' doesn't exist! " );
                    
//  create a temporary parentMenu
                    parentMenu  =   new  MenuComponent();
                    parentMenu.setName(parent);
                    repository.addMenu(parentMenu);
                }

                mc.setParent(parentMenu);
            }
            String title 
=  (String) row.get( " title " );
            mc.setTitle(title);
            String location 
=  (String) row.get( " location " );
            mc.setLocation(location);
            String description 
=  (String) row.get( " description " );
            mc.setDescription(description);
            repository.addMenu(mc);
        }
        
        request.setAttribute(
" examplesRepository " , repository);
    }

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