Check if there is a request url for the current page so that you can enable / disable the menu link

How can I check if I have a specific url? I have a menu with many links, and I would like to disable the link for the current page. How can i achieve this?

+5
source share
2 answers

To get the current page URL, you can use:

Relative Path: <%= request.getServletPath() %>

Can you clarify how you want to disable some links?

+4
source

HttpServletRequest#getRequestURI() URI . getServletPath(), , , ( URL JSP/), URI ( ). JSP - , JSP , .

, , List<Page> , Page url name, JSTL:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<ul id="menu">
    <c:forEach items="${menu}" var="page">
        <c:set var="active" value="${fn:endsWith(pageContext.request.requestURI, page.url)}" />
        <li class="${active ? 'active' : 'none'}"><a href="${page.url}">${page.name}</a></li>
    </c:forEach>
</ul>

.active. . , ..

ul#menu li {
    background: white;
}

ul#menu li.active {
    background: pink; 
}
+17

All Articles