开发者

JSP mechanism for nav bar template/module

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜