JSP and XML
Hi all,
Need a suggestion for doing this
In our application we have security issues
so depending on the role the index page for the user will change
that means user can see only the menus that are provided for him
we want to display the menus that are only enabled for him
so i have setup a xml file with the configuration of what role will have what menus and submenus.
so i want to pull the data from the xml file and display it on the jsp page.
so i need to display the menus as tabs on the jsp page.
suggest me an idea of how to do this.
[602 byte] By [
texasa] at [2007-11-26 18:42:06]

# 1
Have you started implementing the overall solution?
What part do you need to know?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To display menus as tabs you can make use of HTML list items and then use CSS to display them inline (horizontal)
Take a look at :
http://alistapart.com/articles/horizdropdowns and at
http://css.maxdesign.com.au/listutorial/horizontal_master.htm for that
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To display data from the XML there are 2 options if you are using
JSTL 1.1
1) Use XSLT to transform the XML into HTML list items, and call the transformation from the JSP using JSTL <x:transform tag
2) Parse the XML into the JSP using ><x:parse and write a ><c:for loop inside the JSP to display the menu items.
I would recommend option 1) as it would modularize your code, and cleanly separate logic to XSLT. Also, in case there are future enhancements then XSLT is better equipped to handle those.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I don't know how to handle role-based login. Perhaps you could use JAAS for that and combine it with MVC to do the routing based on role.>
# 2
hi thanx for the reply
I have xml file like this
<RoleList>
<Role>
<RoleName>Manager</RoleName>
<Menu>WorkQueue</Menu>
<Menu>Reports
<MenuItem>Anal
<SubMenuItem>Executive</SubMenuItem>
<SubMenuItem>Country</SubMenuItem>
<SubMenuItem>Shared</SubMenuItem>
</MenuItem>
<MenuItem>Listing
<SubMenuItem>Name</SubMenuItem>
<SubMenuItem>Country</SubMenuItem>
<SubMenuItem>Region</SubMenuItem>
</MenuItem>
<MenuItem>Reports
<SubMenuItem>NHTSA</SubMenuItem>
<SubMenuItem>Nomenclature</SubMenuItem>
<SubMenuItem>Change</SubMenuItem>
</MenuItem>
</Menu>
</Role>
<Role>
<RoleName>Planner</RoleName>
<Menu>Work</Menu>
<Menu>Plan
<MenuItem>Wor</MenuItem>
<MenuItem>Plan</MenuItem>
<MenuItem>History</MenuItem>
</Menu>
</Role>
</RoleList>
I need to display the menu tags on the jsp page as menus on the page
# 4
I tried to implement some code using XSLT here's something I tried but it doesn't fully work. You can improve upon it once you have the idea.
Also check with XSLT's mailing list for a better / working XSLT solution:
http://www.mulberrytech.com/xsl/xsl-list/ if you want to go the route of XSLT.
I know the JSP/JSTL code below works, just the XSLT code needs tweaking:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index.jsp (uses JSTL 1.1 - you must install and configure)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head><title>Sample Role Based Menu</title></head>
<body>
<c:set var="LoggedInRoleName" value="Manager"/>
<c:import url="role_menus.xml" var="xml"/>
<c:import url="make_menu.xsl" var="xsl"/>
<x:transform doc="${xml}" xslt="${xsl}">
<x:param name="RoleNameParam" value="${LoggedInRoleName}"/>
</x:transform>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
role_menus.xml - I modified it so that each data item is contained with an XML element - perhaps it is possible to refer to the text() node under an XML node, but I don't know how at this time.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?xml version="1.0" encoding="UTF-8" ?>
<RoleList>
<Role>
<RoleName>Manager</RoleName>
<Menu>
<MainMenu>WorkQueue</MainMenu>
</Menu>
<Menu>
<MainMenu>Reports</MainMenu>
<MenuItem>
<FirstLevelItem>Anal</FirstLevelItem>
<SubMenuItem>Executive</SubMenuItem>
<SubMenuItem>Country</SubMenuItem>
<SubMenuItem>Shared</SubMenuItem>
</MenuItem>
<MenuItem>
<FirstLevelItem>Listing</FirstLevelItem>
<SubMenuItem>Name</SubMenuItem>
<SubMenuItem>Country</SubMenuItem>
<SubMenuItem>Region</SubMenuItem>
</MenuItem>
<MenuItem>
<FirstLevelItem>Reports</FirstLevelItem>
<SubMenuItem>NHTSA</SubMenuItem>
<SubMenuItem>Nomenclature</SubMenuItem>
<SubMenuItem>Change</SubMenuItem>
</MenuItem>
</Menu>
</Role>
<Role>
<RoleName>Planner</RoleName>
<Menu>
<MainMenu>Work</MainMenu>
</Menu>
<Menu>
<MainMenu>Plan</MainMenu>
<MenuItem>
<FirstLevelItem>Wor</FirstLevelItem>
</MenuItem>
<MenuItem>
<FirstLevelItem>Plan</FirstLevelItem>
</MenuItem>
<MenuItem>
<FirstLevelItem>History</FirstLevelItem>
</MenuItem>
</Menu>
</Role>
</RoleList>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make_menu.xsl - you can simplify this XSL just to see some output
in the JSP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="RoleNameParam" select="DefaultRole"/>
<xsl:template match="/">
<xsl:apply-templates select="RoleList"/>
</xsl:template>
<xsl:template match="RoleList">
<ul>
<xsl:apply-templates select="Role[RoleName = $RoleNameParam]/Menu/MainMenu"/>
</ul>
</xsl:template>
<xsl:template match="MainMenu">
<li>
<xsl:value-of select="MainMenu"/>
<xsl:apply-templates select="MenuItem"/>
</li>
</xsl:template>
<xsl:template match="MenuItem">
<ul>
<xsl:apply-templates select="FirstLevelItem"/>
</ul>
</xsl:template>
<xsl:template match="FirstLevelItem">
<li>
<xsl:value-of select="FirstLevelItem"/>
<xsl:apply-templates select="../SubMenuItem"/>
</li>
</xsl:template>
<xsl:template match="SubMenuItem">
<li>
<xsl:value-of select="SubMenuItem"/>
</li>
</xsl:template>
</xsl:stylesheet>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have one suggestion for your XML menu strucutre, instead of
having a named node for each menu item you could change it to
something like this:
<?xml version="1.0" encoding="UTF-8">
<RoleList>
<Role>
<RoleName>Manager</RoleName>
<Menu>
<MenuItem>FirstMenu Item</MenuItem>
<MenuItem>
<Menu>
<MenuItem>Second Menu Item</MenuItem>
</Menu>
</MenuItem>
</Menu>
</Role>
</RoleList>
and then perhaps recursively call the xsl templates to print menu items to any depth. This is just in theory, I'm not sure if it will work but you can check with the XSLT mailing list for possible solutions.