Example Code
This section cites code from Navigation API Portlet from the Quickstarts Collection.
pom.xml
There is only one noticeable difference in pom.xml against the general case presented in Starting a Portlet Project: we only need to add the gatein-api dependency:Code Snippet error: Unable to retrieve the URL: https://github.com/gatein/gatein-portal-quickstart/raw/master/navigation-api-portlet/pom.xml status code: 404.
The Java Part
There are only two classes necessary to implement the Navigation Portlet: NavigationPortlet and NavigationNodeBean.
The NavigationPortlet.doView(RenderRequest, RenderResponse) method is responsible for the rendering of the main, always visible menu.Code Snippet error: Unable to retrieve the URL: https://github.com/gatein/gatein-portal-quickstart/raw/master/navigation-api-portlet/src/main/java/org/gatein/portlet/quickstart/navigation/NavigationPortlet.java status code: 404.
The NavigationPortlet.serveResource(ResourceRequest, ResourceResponse) method serves the AJAX requests to render sub-menus. For a given sub-menu URI, it returns a HTML fragment containing the sub-menu.Code Snippet error: Unable to retrieve the URL: https://github.com/gatein/gatein-portal-quickstart/raw/master/navigation-api-portlet/src/main/java/org/gatein/portlet/quickstart/navigation/NavigationPortlet.java status code: 404.
The NavigationNodeBean serves as a model and contains all the information being rendered on the page. In the NavigationPortlet.doView() method, the navigationRootNodeBean field is representing the root node of the navigation and contains main menu (top-menu) elements (Home and Sitemap by default) as children nodes.
The JSP Part
We have chosen to use JSP (JavaServer Pages) for rendering.
The navigation.jsp file is encapsulates the node.jsp output into an element with unique id identifier:Code Snippet error: Unable to retrieve the URL: https://github.com/gatein/gatein-portal-quickstart/raw/master/navigation-api-portlet/src/main/webapp/jsp/navigation.jsp status code: 404.
The node.jsp file is responsible for rendering the menus. Please note, that the HTML comment tag does not disable the JSP code, and it is used just to get rid of the whitespace for more precise CSS styling:Code Snippet error: Unable to retrieve the URL: https://github.com/gatein/gatein-portal-quickstart/raw/master/navigation-api-portlet/src/main/webapp/jsp/node.jsp status code: 404.
The JavaScript Part
The JavaScript part was implemented as a JQuery plugin. It has several roles:
-
AJAX support
-
Basic on-click drop-down support
-
Dynamically respond to window changes, position menus within the browser window
The asynchronous requests use the JQuery ajax() method:Code Snippet error: Unable to retrieve the URL: https://github.com/gatein/gatein-portal-quickstart/raw/master/navigation-api-portlet/src/main/webapp/js/dropdown.jquery.js status code: 404.
For more details about the implementation, please see the comments in the (dropdown.jquery.js) file.