使用JSTL创建带有导航链接的菜单 [英] Creating a menu with navigation links with JSTL

查看:71
本文介绍了使用JSTL创建带有导航链接的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用JSTL创建带有导航链接的菜单的库或最佳实践方法?

Is there a library or best-practice way of creating a menu with navigation links using JSTL?

我在每个页面上都有5个链接.我希望指向当前页面的链接被禁用".我可以手动执行此操作,但这必须是人们以前解决过的问题.如果有一个可处理它的taglib,但我不知道,我不会感到惊讶.

I have 5 links that go on every page. I want the link that points to the current page to be "disabled". I can do this manually but this must be a problem that people have tackled before. I wouldn't be surprised if there is a taglib that handles it but I don't know of it.

推荐答案

您可以让JSTL/EL根据请求的JSP页面的URL有条件地生成HTML/CSS.您可以通过EL中的${pageContext.request.servletPath}来获得它.假设您在应用程序范围的某些Map<String, String>中具有链接:

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

<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>

或者当您刚接触CSS类

Or when you're just after a 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>

您可以使用<jsp:include>重用JSP页面中的内容.将以上内容放入其自己的menu.jsp文件中,并包括以下内容:

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

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

该页面位于WEB-INF文件夹中,以防止直接访问.

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

这篇关于使用JSTL创建带有导航链接的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆