Thursday, September 11, 2008

Struts-Menu First Impression

We will talk about Struts-Menu tag library. It's demo can be seen in here and it's library can be downloaded in here. We will integrate it in AppFuse. Before we start, you must do this first. We will try build a simple menu from a database table. It's demo can be seen in here. In our that project (AppfuseExample), by default we can add menu and menu item in menu-config.xml. But how we can add dynamic driven menu from database? Okay, we will start here. There are 3 tables by default in our database (NDUNGDB). They are app_user, role, and user_role. We will add 2 tables more for our configuration menu. They are menu and role_menu. Table menu will store menus and table role_menu will store which menus can be accessed by which roles. Table menu will have 4 columns they are id, name, url, and parent. In other hand role_menu table will have 2 columns. They are role_id and menu_id.

DDL SQL file for table menu and role_menu

Edit table menu and role_menu. Add fields for them. Here is my menu and role_menu table based on default configuration of AppFuse. I'm using MySQL Query Browser.

menu table.


role_menu table.

We will add persistence model for menu by mapping table menu and edit role model in addition role_menu mapping table. In package com.ndung.model, add file Menu.java and edit Role.java by adding attribute menus.

Menu.java


Role.java (edit)

In applicationContext-hibernate.xml add mapping model for menu. Add this line in mapping resources.

applicationContext-hibernate.xml (edit)

We will add a filter in package com.ndung.webapp.filter named AuthenticationProcessingFilter by extend AcegiSecurity default filter. This filter is used to store all menus (menu names and it's url) from database and all roles that can access those menus in repository. Then, edit security.xml by adding this filter.

AuthenticationProcessingFilter.java


security.xml (edit)

We will use ListMenu. Add mapping menu in menu-config.xml. Add this line in displayers.

menu-config.xml (edit)

In folder web/pages add jsp file named menu.jsp (the other menu.jsp file is in folder web/common) and in folder web/decorators/ backup our default.jsp and edit it like this. Also you must have to need menuExpandable.css and menuExpandable.js. Place menuExpandable.css in folder web/styles and menuExpandable.js in folder web/scripts.

menu.jsp


default.jsp

Add these lines in ApplicationResources.properties.

ApplicationResources.properties (edit)

Here is some screenshot after we login as mraible. We must add a default css for a better view for sure.


4 comments:

Anonymous said...

hi can you please tell which class is that Manager class in AuthenticationProcessingFilter.java file?

Anonymous said...

hi can you please tell which class is that Manager class in AuthenticationProcessingFilter.java file?

Anonymous said...

Hey All,

I have been looking to figure out a good place to begin the acai free trial and was wondering if someone had some opinions on whether or not the stuff works for dieting? So far this is the one [url=http://acaiberries.zoomblog.com/]article[/url] I've been able to read that seemed real for the goals I have. Thoughts?

Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards