简体   繁体   中英

Passing value to JSP in spring

I have three tabs with href links,

<ul >
        <li class="active">
            <a href="#tab_0" id="tab1" data-toggle="tab">Search by Booking ID</a>
        </li>
        <li>
            <a href="#tab_1" id="tab2" data-toggle="tab">Today's Booking</a>
        </li>
        <li>
            <a href="#tab_2" id="tab3" data-toggle="tab">Search by Date</a>
        </li>
</ul>

When submitting the form in each tab, I'm passing a value to server to know which tab I've selected, using a hidden input field < input type=hidden value="tab1" id="view"/>. What I want is to get the same value on the jsp page and select the corresponding tab, on page load. I'm using Spring framework. I tried adding an additional object in ModelAndView, like

ModelAndView model = new ModelAndView();
model.addObject("view", view);

And script code is:

$(document).ready(function() {
       var activeTab = ${view};

       if(activeTab == "tab1"){
           $("#tab1").click();

       } else if(activeTab == "tab2"){
           $("#tab2").click();

       }else if(activeTab == "tab3"){
           $("#tab3").click();

       }else{
            $("#tab1").click();
       }
    }); 

But this is not working. :( Please help.

You don't need the JavaScript code. since you are already adding the view value in your response, you can use el just to enforce the active css class in your JSP file for the specified tab, like this

<ul >
    <li ${view eq "tab1"?'class="active"':''}>
        <a href="#tab_0" id="tab1" data-toggle="tab">Search by Booking ID</a>
    </li>
    <li ${view eq "tab2"?'class="active"':''}>
        <a href="#tab_1" id="tab2" data-toggle="tab">Today's Booking</a>
    </li>
    <li ${view eq "tab3"?'class="active"':''}>
        <a href="#tab_2" id="tab3" data-toggle="tab">Search by Date</a>
    </li>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM