Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6030079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:59:00+00:00 2026-05-23T04:59:00+00:00

I have this nav code in multiple JSP files: <ul id=nav> <li ><a href=/home>Home</a></li>

  • 0

I have this nav code in multiple JSP files:

<ul id="nav">
    <li ><a href="/home">Home</a></li>
    <li class="active" ><a href="/bills">Bills</a></li>
    <li ><a href="/invoices">Invoices</a></li>
</ul>

What’s the best way to abstract out this code and make the selection of the active nav element programmatic rather than manually defined in the html?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:59:01+00:00Added an answer on May 23, 2026 at 4:59 am

    Use <jsp:include> to include reuseable JSP code fragments.

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

    Use JSTL/EL to dynamically control HTML output.

    <ul id="nav">
        <c:forEach items="${pages}" 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>
    

    Use a Javabean class to represent a model which can be used throughout all code layers.

    public class Page {
    
        private String url;
        private String name;
    
        // Add/generate getters, setters and other boilerplate.
    }
    

    Use a ServletContextListener to preload applicationwide data.

    @WebListener
    public class Config implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            List<Page> pages = new ArrayList<Page>();
            pages.add(new Page("/home", "Home"));
            pages.add(new Page("/bills", "Bills"));
            // ...
            event.getServletContext().setAttribute("pages", pages);
        }
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code : <a href=javascript:void(0) id=0 class=hp_nav_label>0</a> <a href=javascript:void(0) id=1 class=hp_nav_label>1</a> <a
I have this code: <div class=container id=container> <div class=content id=content> <div class='nav'> <ul> <li><a
I have this bit of code: $(body.ow-client1 #stage-nav li.nav1 a).addClass(sub-nav-active); $(body.ow-client2 #stage-nav li.nav2 a).addClass(sub-nav-active);
I have a basic website nav layout that looks like this: <li class=folder parent_folder>
I have this code: #left-nav #left{ width:100px; height: 99px; float:left; border-bottom: 1px solid #344a53;
I have this code on my print.css: #header, #tae, #nav, .noprint {display: none;} width:
I have this code which calculate route between tow places function createDrivingRoute() { $(#nav).html();//CLEAR
I have this code in my html.erb at many places. <div id=left-nav> <%= render
Im using the 960 css framework. I have this code: <div class=container_16 id=header> div
I have this code in jQuery, that I want to reimplement with the prototype

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.