该类是Struts1.2中的一个Action,该类的功能是根据页面传来的参数返回给Menu对象添加一个页面地址的字符串, 
而且在返回该字符串之前先要进行管理员身份的测试。 
以下是该类没有改造前的代码: 
public class TopMenuAction extends DispatchAction { 
    private IChannelService channelService; 
    private IUserDao userDao;     public void setUserDao(IUserDao userDao) { 
        this.userDao = userDao; 
    }     public IChannelService getChannelService() { 
        return channelService; 
    }     public void setChannelService(IChannelService channelService) { 
        this.channelService = channelService; 
    }     public ActionForward menulist(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 
            HttpServletResponse response) { 
        String uid = (String) request.getSession().getAttribute("uid"); 
        List<ViewAuthChannelForm> channels = null; 
        List<Channel> admingchannel = null; 
        String subpath = request.getParameter("subpath"); 
        List<Menu> menuList = null; 
        try { 
            menuList = new ArrayList<Menu>(); 
            if (userDao.isAdministrator(uid)) { 
                admingchannel = channelService.getChannelByParnetId(uid, "0"); 
                for (Channel c : admingchannel) { 
                    Menu menu = new Menu(); 
                    menu.setMenu_id(c.getId()); 
                    menu.setMenu_order_num(c.getOrderNum()); 
                    if (subpath != null && !subpath.equals("") && subpath.trim().split(",")[0].equals(menu.getMenu_id())) { 
                        menu.setBackground("/images/dh_b.gif"); 
                        request.setAttribute("subpath", subpath); 
                    } 
                    if (menu.getMenu_id().equals("1")) { 
                        menu.setMenu_url("/jsp/gzzd/index.jsp"); 
                    } 
                    if (menu.getMenu_id().equals("2")) { 
                        menu.setMenu_url("/jsp/tztg/index.jsp"); 
                    } 
                    if (menu.getMenu_id().equals("3")) { 
                        menu.setMenu_url("/jsp/gzdt/index.jsp"); 
                    } 
                    if (menu.getMenu_id().equals("4")) { 
                        menu.setMenu_url("/jsp/zhfw/zhfwindex.jsp"); 
                    } 
                    if (menu.getMenu_id().equals("5")) { 
                        menu.setMenu_url("/jsp/zxxx/index.jsp"); 
                    } 
                    menu.setChannelName(c.getChannelName()); 
                    menuList.add(menu); 
                } 
            } 
            else { 
                channels = channelService.getTopMenuByUser(uid, "0", "0"); 
                if (channels != null) { 
                    for (int i = 0; i < channels.size(); i++) { 
                        ViewAuthChannelForm c = channels.get(i); 
                        Menu menu = new Menu(); 
                        menu.setMenu_id(c.getChannel_id()); 
                        menu.setMenu_order_num(c.getChannel_order_num()); 
                        if (subpath != null && !subpath.equals("") && subpath.trim().split(",")[0].equals(menu.getMenu_id())) { 
                            menu.setBackground("/images/dh_b.gif"); 
                            request.setAttribute("subpath", subpath); 
                        }                      
                        if (menu.getMenu_id().equals("1")) {                             menu.setMenu_url("/jsp/gzzd/index.jsp"); 
                        } 
                        if (menu.getMenu_id().equals("2")) { 
                            menu.setMenu_url("/jsp/tztg/index.jsp"); 
                        } 
                        if (menu.getMenu_id().equals("3")) { 
                            menu.setMenu_url("/jsp/gzdt/index.jsp"); 
                        } 
                        if (menu.getMenu_id().equals("4")) { 
                            menu.setMenu_url("/jsp/zhfw/zhfwindex.jsp"); 
                        } 
                        if (menu.getMenu_id().equals("5")) { 
                            menu.setMenu_url("/jsp/zxxx/index.jsp"); 
                        } 
                        menu.setChannelName(c.getChannel_name()); 
                        menuList.add(menu); 
                    } 
                } 
            } 
            request.getSession().setAttribute(CebCmsConsts.MENULIST, menuList); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
        request.setAttribute("channelid", Consts.CHANNEL_ID_GZZD); 
        return mapping.findForward("menu"); 
    } 
} 在一位有4年工作经验的程序员的指挥下,我改出了以下代码。 
但是,我觉得改出来的代码还是有问题,您可以往下看。 
我希望各位大哥大姐、老师们可以再动手改一改,因为值得改的地方太多了。 
public class TopMenuAction extends DispatchAction {     private IChannelService channelService; 
    private IUserDao userDao;     public ActionForward menulist(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 
            HttpServletResponse response) { 
        GenerateMenuList generate = null; 
        String uid = (String) request.getSession().getAttribute("uid"); 
        List<ViewAuthChannelForm> channels = null; 
        List<Channel> admingchannel = null; 
        String subpath = request.getParameter("subpath"); 
        List<Menu> menuList = null; 
        try { 
            menuList = new ArrayList<Menu>(); 
            if (userDao.isAdministrator(uid)) { 
                generate = new AdminMenuList(); 
                admingchannel = channelService.getChannelByParnetId(uid, "0"); 
                menuList = generate.generateMenuList(menuList, admingchannel, subpath, request); 
            } 
            else { 
                channels = channelService.getTopMenuByUser(uid, "0", "0"); 
                if (channels != null) { 
                    generate = new UnAdminMenuList(); 
                    menuList = generate.generateMenuList(menuList, channels, subpath, request); 
                } 
            } 
            request.getSession().setAttribute(Consts.MENULIST, menuList); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
        request.setAttribute("channelid", Consts.CHANNEL_ID_GZZD); 
        return mapping.findForward("menu"); 
    } 
   
    private List administrator(){ 
        return null; 
    }     public void setUserDao(IUserDao userDao) { 
        this.userDao = userDao; 
    }     public void setChannelService(IChannelService channelService) { 
        this.channelService = channelService; 
    } } 
该抽象类把原来处理管理员和非管理员中重复的代码逻辑提炼了一下。 
然后建立两个类,一个管理员的、一个非管理员的。 
public abstract class MenuList<T extends Object, E extends Object> { 
    public abstract List<T> generateMenuList(List<T> menuList, List<E> list, String subpath, HttpServletRequest request);     protected void setImageAndSubpath(String subpath, Menu menu, HttpServletRequest request) { 
        if (isSubpatEffective(subpath, menu)) { 
            menu.setBackground("/images/dh_b.gif"); 
            request.setAttribute("subpath", subpath); 
        } 
    }     private boolean isSubpatEffective(String subpath, Menu menu) { 
        return subpath != null && !subpath.equals("") && subpath.trim().split(",")[0].equals(menu.getMenu_id()) ? true 
                : false; 
    } 

该类就是管理员的执行逻辑,非管理员类似,我就不贴上来了。 
public class AdminMenuList extends GenerateMenuList<Menu, Channel> { 
    private final HomeURL urlAdress = new HomeURL();     public List<Menu> generateMenuList(List<Menu> menuList, List<Channel> admingchannel, String subpath, 
            HttpServletRequest request) { 
        for (Channel c : admingchannel) { 
            Menu menu = new Menu(); 
            Map<String, MenuURLState> homeURLCollection = urlAdress.getHomeURLCollection(); 
            menu.setMenu_id(c.getId());             MenuURLState urlState = homeURLCollection.get(menu.getMenu_id()); 
            menu.setMenu_url(urlState.urlState());             menu.setMenu_order_num(c.getOrderNum()); 
            menu.setChannelName(c.getChannelName()); 
            setImageAndSubpath(subpath, menu, request); 
            menuList.add(menu); 
        } 
        return menuList; 
    } 
} 因为会根据页面返回的值给出一个返回值,所以想用一个状态模式,我觉得虽然用了,但是不彻底,还需要 
大家给出意见。 谢谢。 
public class HomeURL { 
    private Map<String, MenuURLState> homeURLCollection = new HashMap<String, MenuURLState>(); 
    public HomeURL(){ 
        homeURLCollection.put("1", new GzzdURLState()); 
        homeURLCollection.put("2", new TztgURLState()); 
        homeURLCollection.put("3", new GzdtURLState()); 
        homeURLCollection.put("4", new ZhfwURLState()); 
        homeURLCollection.put("5", new ZxxxURLState()); 
    } 
    public Map<String, MenuURLState> getHomeURLCollection(){ 
        return homeURLCollection; 
    } 

这是其中的一个实现的子类,其它类似就不列举了。 
public class GzdtURLState implements MenuURLState{ 
    public String urlState() { 
        return "/jsp/gzdt/index.jsp"; 
    }