春季MVC中的菜单人群

My Current status.

1.Fetch all the menu from DB, as a Map contains key as main menu and set as submenus. 2.While Loading Welcome.jsp, main menu Item will display from key of the map and keep this map in session. 3.on click of main menu I will call an Jquery Ajax call to display the sub menu Item, the same time my color change of main menu is happening. Upto this I have achieved Now the challenge as below 1.If I click on another submenu Item, I dont want to change the main menu status-need to retain the color change. 2.How can I retain this menus in all the pages with minimum effort.

So here what you can do

  1. Call controller to get List of menu and sub menu
  2. In JSP assign class for menu and submenu e.g class="mainLevelMenu" for menu and class="subMenu" for sub menu.
  3. Define separate class="selected" for selected main menu.
  4. When user click on menu item assign this class to the menu item.
  5. For sub menu you can hyperlink to controller. Do ajax call and load the response in body part. That will leave menu portion as it is.

If possible post your code we can go from there.

I would approach this with a class to represent the Menus, and a ControllerAdvice to add this info in all pages needed of your application. The Menu class can be something like:

class Menu{
     String href;
     List<Menu> subitems = new ArrayList();
}

Then when rendering the jsp, you can organize it the way you described.

About the @ControllerAdvice, you can add things to the pages model. It's useful if this menu shows up in a lot of pages.

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html