메뉴코드 |
상위메뉴코드 |
메뉴명 |
1 |
0 |
MainPage |
2 |
1 |
Second Menu 1 |
4 |
2 |
Third Menu 1 |
5 |
2 |
Third Menu 2 |
3 |
1 |
Second Menu 2 |
상단의 테이블 정보를 트리형식으로 표현하게 되면 아래와 같은 구조를 가지고 있음을 알 수 있다.
- MainPage
- Second Menu1
- Third Menu 1
- Third Menu 2
- Second Menu2
최상위 메뉴인 MainPage를 기준으로 하위 페이지 내용들을 출력하기 위하여
1. HashMap을 사용하여 메뉴코드가 중복되지 않는 점을 이용
2. Parent-Child 관계를 확인 후, ChildrenList에 페이지 정보를 추가
3. 메뉴 정보 출력
위 순서대로 구성되어 있다.
public class MenuVO {
private int menuSeq;
private int menuUpSeq;
private String menuNm;
private int menuSno;
private List<MenuVO> childrenList;
public MenuVO(){
super();
}
public MenuVO(MenuVO menuVO){
this.menuSeq = menuVO.getMenuSeq();
this.menuUpSeq = menuVO.getMenuUpSeq();
this.menuNm = menuVO.getMenuNm();
this.childrenList = new ArrayList<MenuVO>();
}
public int getMenuSeq() {
return menuSeq;
}
public void setMenuSeq(int menuSeq) {
this.menuSeq = menuSeq;
}
public int getMenuUpSeq() {
return menuUpSeq;
}
public void setMenuUpSeq(int menuUpSeq) {
this.menuUpSeq = menuUpSeq;
}
public String getMenuNm() {
return menuNm;
}
public void setMenuNm(String menuNm) {
this.menuNm = menuNm;
}
public int getMenuSno() {
return menuSno;
}
public void setMenuSno(int menuSno) {
this.menuSno = menuSno;
}
public List<MenuVO> getChildrenList() {
return childrenList;
}
public void setChildrenList(List<MenuVO> childrenList) {
this.childrenList = childrenList;
}
public void addChildren(MenuVO children){
this.childrenList.add(children);
}
@Override
public String toString() {
return "MenuVO [menuSeq=" + menuSeq + ", menuUpSeq=" + menuUpSeq + ", menuNm=" + menuNm + ", menuSno=" + menuSno
+ ", childrenList=" + childrenList + "]";
}
}
public static void main(String[] args) {
List<MenuVO> menuList = new ArrayList<MenuVO>();
MenuVO topMenu = new MenuVO();
topMenu.setMenuSeq(1);
topMenu.setMenuUpSeq(0);
topMenu.setMenuNm("Main Page");
menuList.add(topMenu);
MenuVO secondMenu1 = new MenuVO();
secondMenu1.setMenuSeq(2);
secondMenu1.setMenuUpSeq(1);
secondMenu1.setMenuNm("secondMenu1 Page");
menuList.add(secondMenu1);
MenuVO thirdMenu1 = new MenuVO();
thirdMenu1.setMenuSeq(4);
thirdMenu1.setMenuUpSeq(2);
thirdMenu1.setMenuNm("thirdMenu1 Page");
menuList.add(thirdMenu1);
MenuVO thirdMenu2 = new MenuVO();
thirdMenu2.setMenuSeq(5);
thirdMenu2.setMenuUpSeq(2);
thirdMenu2.setMenuNm("thirdMenu2 Page");
menuList.add(thirdMenu2);
MenuVO secondMenu2 = new MenuVO();
secondMenu2.setMenuSeq(3);
secondMenu2.setMenuUpSeq(1);
secondMenu2.setMenuNm("secondMenu2 Page");
menuList.add(secondMenu2);
HashMap<Integer, MenuVO> hm = new HashMap<Integer, MenuVO>();
for(MenuVO p:menuList){
MenuVO mmdChild ;
if(hm.containsKey(p.getMenuSeq())){
mmdChild = hm.get(p.getMenuSeq());
}
else{
mmdChild = new MenuVO(p);
hm.put(p.getMenuSeq(),mmdChild);
}
MenuVO mmdParent ;
if(hm.containsKey(p.getMenuUpSeq())){
mmdParent = hm.get(p.getMenuUpSeq());
}
else{
mmdParent = new MenuVO(p);
hm.put(p.getMenuUpSeq(),mmdParent);
}
mmdParent.setMenuSeq(p.getMenuUpSeq());
mmdParent.addChildren(mmdChild);
printHashMap(hm);
}
MenuVO menuVO = hm.get(1);
printMenu(menuVO);
}
public static void printMenu(MenuVO menuVO){
System.out.println("##### START #####");
System.out.println(menuVO.getMenuSeq() + " : " + menuVO.getMenuUpSeq() + " : " + menuVO.getMenuNm());
List<MenuVO> tmp = menuVO.getChildrenList();
if ( !tmp.isEmpty() ){
for (MenuVO childeMenuVO : tmp) {
printMenu(childeMenuVO);
}
}
System.out.println("@@@@@ END @@@@@\n");
}
public static void printHashMap(HashMap<Integer, MenuVO> hm){
Set<Integer> keySet = hm.keySet();
System.out.println("$$$$$ START $$$$$");
for (Integer integer : keySet) {
MenuVO menuVO = hm.get(integer);
System.out.println(menuVO.toString());
}
System.out.println("$$$$$ END $$$$$\n");
}
/*
결과
$$$$$ START $$$$$
MenuVO [menuSeq=0, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[]]]]
MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[]]
$$$$$ END $$$$$
$$$$$ START $$$$$
MenuVO [menuSeq=0, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[]]]]]]
MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[]]]]
MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[]]
$$$$$ END $$$$$
$$$$$ START $$$$$
MenuVO [menuSeq=0, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]]]]]]]]
MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]]]]]]
MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]]]]
MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]]
$$$$$ END $$$$$
$$$$$ START $$$$$
MenuVO [menuSeq=0, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]], MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]]]]]]]
MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]], MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]]]]]
MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]], MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]]]
MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]]
MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]
$$$$$ END $$$$$
$$$$$ START $$$$$
MenuVO [menuSeq=0, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]], MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]]], MenuVO [menuSeq=3, menuUpSeq=1, menuNm=secondMenu2 Page, menuSno=0, childrenList=[]]]]]]
MenuVO [menuSeq=1, menuUpSeq=0, menuNm=Main Page, menuSno=0, childrenList=[MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]], MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]]], MenuVO [menuSeq=3, menuUpSeq=1, menuNm=secondMenu2 Page, menuSno=0, childrenList=[]]]]
MenuVO [menuSeq=2, menuUpSeq=1, menuNm=secondMenu1 Page, menuSno=0, childrenList=[MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]], MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]]]
MenuVO [menuSeq=3, menuUpSeq=1, menuNm=secondMenu2 Page, menuSno=0, childrenList=[]]
MenuVO [menuSeq=4, menuUpSeq=2, menuNm=thirdMenu1 Page, menuSno=0, childrenList=[]]
MenuVO [menuSeq=5, menuUpSeq=2, menuNm=thirdMenu2 Page, menuSno=0, childrenList=[]]
$$$$$ END $$$$$
##### START #####
1 : 0 : Main Page
##### START #####
2 : 1 : secondMenu1 Page
##### START #####
4 : 2 : thirdMenu1 Page
@@@@@ END @@@@@
##### START #####
5 : 2 : thirdMenu2 Page
@@@@@ END @@@@@
@@@@@ END @@@@@
##### START #####
3 : 1 : secondMenu2 Page
@@@@@ END @@@@@
@@@@@ END @@@@@
*/