如何设置class =" active"到活动页面 [英] How to set class="active" to the active page
问题描述
我正在设计一个我正在使用jsp页面的应用程序。现在我需要一些东西来指示哪个是当前正在显示的页面。基本上,我想要一个包含每个页面的菜单,我在当前页面上放置class =active。
I'm designing an application in which I'm using jsp pages. Now I need something that indicates which is the current page that is being displayed. Basically, I want a menu that has for every page, on which I place class="active" on the current page.
我该怎么做?有什么建议?
谢谢!
How do I do this? Any suggestions? Thanks!
推荐答案
Kickoff示例:
Kickoff example:
<c:set var="currentPage" value="${fn:substring(pageContext.request.servletPath, 1, -1)}" />
<ul>
<c:forEach items="${bean.menu}" var="item">
<li><a href="${item.link}"${item.link == currentPage ? ' class="active"' : ''}>${item.name}</li>
</c:forEach>
</ul>
这假定 $ {bean.menu}
是列表<菜单>
,菜单
是一个属性为的bean 和
, name
和 $ {item.link}
返回<$ c $之类的路径名c> home.jsp contact.jsp
(或者如果你使用的是控制器servlet, home
,联系
等。)
This assumes ${bean.menu}
to be a List<Menu>
, the Menu
to be a bean with properties link
and name
and the ${item.link}
to return pathnames like home.jsp
, contact.jsp
(or if you're using a controller servlet, home
, contact
, etc).
fn:substring()
摆脱领先的斜线。 EL中的条件运算符?:
仅在条件为真时打印类,否则不打印。
The fn:substring()
gets rid of the leading slash. The conditional operator ?:
in EL only prints the class when the condition is true, else nothing.
这篇关于如何设置class =" active"到活动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!