How To / Build your own menu with Jaffa |
This document will help you to build a menu for your application using JAFFA's Menu component.
JAFFA's menu component can be used to create simple menus or complex menus implementing
JAFFA's Security layer. This document will not cover the Application Security Layer in detail.
Please refer to the Application Security document for
more information on how to use the Security Layer.
The menu component is very simple to use and most of its aspects are user-configurable and customizable.
It uses XML to define the menu structure (including the graphics for the menu items).
The menu component uses JavaBeans to read in the XML file and store the menu structure in a HashMap
variable. This variable can be accessed from a JSP!
The example JSPs that we will use to build the menu are not a part of the JAFFA project but are
available as 'templates' which can be used for customisation to suit your needs.
In the example JSPs that we will use, the "useBean" tag is used with
scope="session". This means that the JavaBean instance is stored in the HttpSession
object and can be used across HTTP requests.
Contents
- Create the XML file defining the Menu
- Create JSPs to use the Menu Component
- Link it all together and Test your menu
- Link the Menu to the Security
Assumptions
- If you are building a menu that will support security using JAFFA's security, it is assumed that you have created the
necessary XML files (for eg, roles.xml, business-functions.xml etc) and have the security architecture in place.
- It is assumed that you have read the various getting started documents that show how to download and install the JAFFA software.
- It is assumed that you have already created a sample application or have some idea about the way that JAFFA can be used
to build an application.
- It is assumed that you have your menu designed alongwith the icons to be displayed for each submenu and menu item.
|
Create the XML file defining the Menu |
- Create the graphics for the icons to represent the menu groups or submenus and for the
menu items to the appropriate directory structure of your application.
- Create menu-list.xml using the sample menu-list.xml providing
details like menu group name, icon, option name, option icon, the component or URL that should be run.
The structure of the XML (as defined by the DTD) is...
menu-list_1_0.dtd |
<!ELEMENT menu (group+)>
<!ELEMENT group (group-name,description,group-icon,title,option+)>
<!ELEMENT group-name (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT group-icon (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT option (option-name,option-icon,(component | url))>
<!ELEMENT option-name (#PCDATA)>
<!ELEMENT option-icon (#PCDATA)>
<!ELEMENT component (#PCDATA)>
<!ELEMENT url (#PCDATA)>
|
The sample file has a mix of component and url attributes for each option element.
Each element can have either a component or a url but not both.
Here is an example of the XML file
menu-list.xml |
<?xml version="1.0" encoding="ISO-8859-1"?><BR>
<menu><BR>
<group><BR>
<group-name>Orders</group-name><BR>
<description>Click on Open Orders for orders that are open and on Shipped Orders
for orders that have been shipped.</description><BR>
<group-icon>ordersIcon.gif</group-icon><BR>
<title>Orders</title><BR>
<option><BR>
<option-name>Open Orders</option-name><BR>
<option-icon>openOrders.gif</option-icon><BR>
<component>Orders.OpenOrder</component><BR>
</option><BR>
<option><BR>
<option-name>Shipped Orders</option-name><BR>
<option-icon>shippedOrders.gif</option-icon><BR>
<component>Orders.ShippedOrder</component><BR>
</option><BR>
</group><BR>
<group><BR>
<group-name>Administration</group-name><BR>
<description>Perform Administrative Tasks</description><BR>
<group-icon>administrator.gif</group-icon><BR>
<title>Administration</title><BR>
<option><BR>
<option-name>Users</option-name><BR>
<option-icon>users.gif</option-icon><BR>
<component>Administrations.Users</component><BR>
</option><BR>
</group><BR>
</menu>
|
- By default, menu-list.xml will be loaded from the classpath:///resources/menu-list.xml
location. If you wish to specify some other location, define property framework.menu.url=
in the framework.properties file. For the purpose of this exercise, create or copy
menu-list.xml file to the "/source/java/resources" directory of your application.
|
Create JSPs to use the Menu Component |
- The JavaBean - MainMenu
MainMenu.class is the JavaBean used to load the XML file into a LinkedHashMap based on security.
Only those components that the User logged in has access to are added to the hashmap!
MainMenu.java is inside the org.jaffa.components.menu package.
MainMenu also uses 2 other objects - Group and Option. Group contains the details for each
submenu / menu group and Option contains the details for each menu item / option.
Group.java and Option.java are in the org.jaffa.components.menu package.
- For this example, 3 JSPs are created and the menu is implemented using "frames".
Create the JSPs in the "menu" module or a module of your choice.
- Create menu.jsp using the sample menu.jsp in the /source/html/jsp directory
of your application. This JSP is the FRAMESET.
- Create menu1.jsp using sample menu1.jsp. This is the "left" frame where the
submenu headings (or menu groups) will appear.
- Create menu2.jsp using sample menu2.jsp. This is the "right" frame where
the menu options for each submenu will appear. The JSPs use the "useBean" JSP tag to instantiate
the bean. The following code can be found in all the 3 sample JSPs...
Example |
<% jsp:useBean id="menu" class="org.jaffa.components.menu.MainMenu" scope="session" %>
|
The following code shows the methods invoked to load the XML file and create a LinkedHashMap of the menu structure and get the HashMap into a variable using scriptlet code -
Example |
<% menu.determineComp(request, pageContext);
Map mp = menu.getComponents(); %>
|
The variable mp holds the HashMap of the menu structure. Since JavaBean doesnt have access to the
request and pageContext , they have to be passed to the instance of MainMenu class.
The following scriptlet code retrieves each menu group / submenu and builds the tree of icons of the submenus.
This code is executed in menu1.jsp -
Example |
<% Set set = mp.entrySet();
Iterator i = set.iterator();
while (i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
Group group = (Group) me.getKey();
String icon = group.getGroupIcon();
String title = group.getGroupTitle();
String name = group.getGroupName();
} %>
|
The following scriptlet code gets all the menu items for each menu group. This code is executed in menu2.jsp -
Example |
<%
Map mp = menu.getComponents();
Set set = mp.entrySet();
Iterator i2 = set.iterator();
Set set = mp.entrySet();
while (i2.hasNext()) {
Map.Entry me = (Map.Entry)i2.next();
ArrayList al = new ArrayList();
al = (ArrayList) me.getValue();
Iterator it2 = al.iterator();
while (it2.hasNext()) {
Option opt = (Option) it2.next();
String optIcon = opt.getIcon();
String optName = opt.getName();
String optLink = opt.getCompURL();
}
} %>
|
|
- After creating the JSPs, compile the JSPs to make sure that there are no errors.
- You should now be ready to build & deploy your menu! Build your application using an Ant script.
- Deploy the WAR file to the web server.
- Start your web server.
- To test the menu - in your web browser, enter "http://localhost:8080/MyApp/menu/jsp/menu.jsp
- You should see all 3 JSPs loaded in the browsers with frames!
Here is an example of a menu built using JAFFA!
|
Link the Menu to the Security |
The Menu can be configured so that it will implement security based on JAFFA's security system.
Please refer to JAFFA's Application Security
Layer for more information on how to implement role-based security.
All components built using JAFFA are guarded by security, but the Menu JSPs can be invoked directly.
They are not guarded under security.
- You can check if the User has already logged in, using
request.getUserPrincipal()
in Menu.jsp. If the User is already logged in getUserPrincipal should return a value.
If a "null" is returned, an appropriate message can be displayed.
The following code can be found in menu.jsp. If you donot wish to enforce security,
remove the reference to request.getUserPrincipal() ...
Example |
<% if (request.getUserPrincipal() == null) {%>
|
- If a component has been specified in
menu-list.xml , the menu component
(MainMenu.java ) will check if the User logged in has access to that particular
component based on the roles.xml . Please refer to the Application Security Layer
section for more on roles.xml .
If the User doesnt have access to a particular component, the Menu wont display that menu option.
A URL specified in the menu-list.xml doesnt go through any security validation.
|