Creating a menu with navigation links with JSTL

Is there a library or better way to create menus using navigation links using JSTL?

I have 5 links that are placed on each page. I want the link to indicate that the current page will be "disabled". I can do it manually, but it should be a problem that people have previously encountered. I would not be surprised if there is a taglib that processes it, but I do not know about it.

+6
source share
1 answer

You can let JSTL / EL generate HTML / CSS conditionally based on the URL of the requested JSP page. You can get it ${pageContext.request.servletPath} in EL. Assuming you have links in some Map<String, String> in the application area:

 <ul id="menu"> <c:forEach items="${menu}" var="item"> <li> <c:choose> <c:when test="${pageContext.request.servletPath == item.value}"> <b>${item.key}</b> </c:when> <c:otherwise> <a href="${item.value}">${item.key}</a> </c:otherwise> </c:choose> </li> </c:forEach> </ul> 

Or when you are only after the CSS class

 <ul id="menu"> <c:forEach items="${menu}" var="item"> <li><a href="${item.value}" class="${pageContext.request.servletPath == item.value ? 'active' : 'none'}">${item.key}</a></li> </c:forEach> </ul> 

You can use <jsp:include> to reuse content on JSP pages. Put the above into your own menu.jsp file and include it as follows:

 <jsp:include page="/WEB-INF/menu.jsp" /> 

The page is placed in the WEB-INF folder to prevent direct access.

+8
source

Source: https://habr.com/ru/post/887558/


All Articles