导航栏模板/模块的 JSP 机制

发布于 2024-09-18 03:46:26 字数 1752 浏览 5 评论 0原文

我有一个导航栏,对于某些页面,还有一个子导航栏。我想将其作为一个模块,以便我可以轻松导入每个页面上的导航栏。只需将代码放入 JSP 文件中并调用即可轻松完成:

<%@include file="/WEB-INF/jsp/includes/sub_nav_bar.jsp"%>

唯一的问题是链接及其标签会根据您所在的页面而变化。因此,我的选择是为不同的页面组创建单独的导航栏,或者传入表示链接和标签的变量。单独的导航栏并不有吸引力,因为有很多变化,特别是子导航栏。我最终会得到大量的导航栏,所有这些我都必须维护。

变量方法有效,但我希望有更好的方法来做到这一点。它是如此混乱和冗长。如果我以后想有下拉菜单,那就更糟糕了。基本上,我已经为此做了一些事情:

<c:set var="subNav" value="1"/>
<c:set var="subNavLink1" value="/"/>
<c:set var="subNavLabel1" value="Home"/>
<c:set var="subNavLink2" value="/fun"/>
<c:set var="subNavLabel2" value="Fun Stuff"/>
<c:set var="subNavLink3" value="/more"/>
<c:set var="subNavLabel3" value="More Stuff"/>
<%@include file="/WEB-INF/jsp/includes/sub_nav_bar.jsp"%>

sub_nav_bar.jsp 包含文件看起来相当难看:

<ul id="sub-nav">
    <li class="<c:if test="${subNav!='1'}">in</c:if>active"><a href="<c:url value='${subNavLink1}'/>">${subNavLabel1}</a></li>
    <c:if test="${subNavLink2!=null}"><li class="<c:if test="${subNav!='2'}">in</c:if>active"><a href="<c:url value='${subNavLink2}'/>">${subNavLabel2}</a></li></c:if>
    <c:if test="${subNavLink3!=null}"><li class="<c:if test="${subNav!='3'}">in</c:if>active"><a href="<c:url value='${subNavLink3}'/>">${subNavLabel3}</a></li></c:if>
    <c:if test="${subNavLink4!=null}"><li class="<c:if test="${subNav!='4'}">in</c:if>active"><a href="<c:url value='${subNavLink4}'/>">${subNavLabel4}</a></li></c:if>
</ul>

那么​​有更好的方法吗?

I have a navigation bar and, for some pages, a sub-navigation bar as well. I'd like to make this a module so I can easily import the navigation bars on each page. Easy enough to simply put the code in a JSP file and call:

<%@include file="/WEB-INF/jsp/includes/sub_nav_bar.jsp"%>

the only issue is the links and their labels change depending on what page you're on. So my options are to create separate navigation bars for various groups of pages or pass in variables representing the links and labels. Separate navigation bars isn't attractive because there are so many variations, particularly with the sub nav bars. I'll end up with tons of navigation bars, all of which I have to maintain.

The variables approach works, but I'm hoping there's a better way to do this. It's so messy and lengthy. If I want to later have drop down menus, it's going to be even much worse. Basically, I've done something to this effect:

<c:set var="subNav" value="1"/>
<c:set var="subNavLink1" value="/"/>
<c:set var="subNavLabel1" value="Home"/>
<c:set var="subNavLink2" value="/fun"/>
<c:set var="subNavLabel2" value="Fun Stuff"/>
<c:set var="subNavLink3" value="/more"/>
<c:set var="subNavLabel3" value="More Stuff"/>
<%@include file="/WEB-INF/jsp/includes/sub_nav_bar.jsp"%>

the sub_nav_bar.jsp include file looks pretty ugly:

<ul id="sub-nav">
    <li class="<c:if test="${subNav!='1'}">in</c:if>active"><a href="<c:url value='${subNavLink1}'/>">${subNavLabel1}</a></li>
    <c:if test="${subNavLink2!=null}"><li class="<c:if test="${subNav!='2'}">in</c:if>active"><a href="<c:url value='${subNavLink2}'/>">${subNavLabel2}</a></li></c:if>
    <c:if test="${subNavLink3!=null}"><li class="<c:if test="${subNav!='3'}">in</c:if>active"><a href="<c:url value='${subNavLink3}'/>">${subNavLabel3}</a></li></c:if>
    <c:if test="${subNavLink4!=null}"><li class="<c:if test="${subNav!='4'}">in</c:if>active"><a href="<c:url value='${subNavLink4}'/>">${subNavLabel4}</a></li></c:if>
</ul>

So is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2024-09-25 03:46:26

您有什么理由坚持使用普通 JSP/JSTL 吗?大多数 Web 框架都会为您简化这项工作。关键是要有一个模板系统,如tiles、sitemesh、facelet、freemarker……

Any reason for you to stick to plain JSP/JSTL? Most web frameworks would simplify this work for you. The key would be to have a templating system like tiles, sitemesh, facelet, freemarker, ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文