iCode
iCode

Reputation: 9202

Set class attribute for a tag in JSP using session attribute from Servlet

How to set a class attribute if some condition is true in a tag in JSP? In my JSP page I have some tabs. In the first tab I have a form field and after submission, it will call servlet, process it and then it will forward to the same JSP page and will set a session attribute value (say the id of next Tab) And in my JSP page in the LI tag I am setting the class attribute to active if the string got from session attribute is some value. But I am not able to get it.

Here is part of servlet code

String dataloadType=request.getParameter("dataloadType");
if(dataloadType.equals("fromDB"))
        {
            request.setAttribute("activeTab", "fromDatabase");
            RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);
        }

Part of index.jsp is

<div class="navbar btn-navbar">
            <div id="tabs" class="tabbable">
                <ul id="myTab" class="nav nav-tabs">
                    <li><a href="#datacollector" target="main"
                        data-toggle="tab">Data Collector</a></li>
                    <li id="fromDB" class="selectDataloadType <c:if test="${activeTab == 'fromDatabase'}">active</c:if>" style="display: none;"><a
                        href="#fromDatabase" target="main" data-toggle="tab">Data Load
                            Database</a></li>
                    <li id="fromFile" class="selectDataloadType" style="display: none;"><a
                        href="#fromFiles" target="main" data-toggle="tab">Data Load
                            File</a></li>
                    <li id="email" class="selectDataloadType" style="display: none;"><a
                        href="#fromEmail" target="main" data-toggle="tab">Data Load
                            Email</a></li>
                    <li id="webServices" class="selectDataloadType"
                        style="display: none;"><a href="#fromWebServices"
                        target="main" data-toggle="tab">Data Load Web</a></li>
                    <li><a href="#datamap" target="main" data-toggle="tab">Data
                            Map</a></li>
                    <li><a href="#schedule" target="main" data-toggle="tab">Schedule</a></li>
                </ul>

LI with id fromDB is setting the class attribute to active if the session attribute is 'fromDatabase' But its not working as it is not taking that part as code. Here is the index.jsp

enter image description here

It is showing the code in page, so its not taking it. How can I solve it?

Upvotes: 0

Views: 7796

Answers (1)

MikO
MikO

Reputation: 18741

I think you're not setting the class attribute correctly... The class attribute of the LI tag is selectDataloadType [space] active, so eventually it is just selectDataloadType I think...

Why not something like:

<li id="fromDB" <c:if test="${activeTab == 'fromDatabase'}">class="active"</c:if>...

You can use choose instead of if if you need to have a class attruibute in any case...

Upvotes: 1

Related Questions